-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
103 lines (89 loc) · 3.01 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const { PDFDocument } = require("pdf-lib");
const fs = require("fs").promises; // Use the promise-based version of fs
const _pdf2img = require("./pdf2img");
const rimraf = require("rimraf");
const pathModule = require("path");
class Flattener {
async flatten(buffer, options = {}) {
const basePath = options.path || __dirname;
const timestamp =
Date.now() +
Math.random().toString(36).substring(2, 6) +
Math.random().toString(36).substring(2, 6);
const path = pathModule.join(basePath, timestamp);
await fs.mkdir(path, { recursive: true });
await fs.mkdir(pathModule.join(path, "docs"), { recursive: true });
const pdf2img = new _pdf2img();
pdf2img.setOptions({
type: options.type || "png",
density: options.density || 200,
outputdir: pathModule.join(path, "split"),
outputname: "split",
page: null,
});
await fs.writeFile(
pathModule.join(path, "docs", "originalFile.pdf"),
buffer
);
try {
await pdf2img.convert(pathModule.join(path, "docs", "originalFile.pdf"));
const splitFiles = await fs.readdir(pathModule.join(path, "split"));
const sortedSplitFiles = splitFiles
.map((file) => pathModule.join(path, "split", file))
.sort(async (a, b) => {
const statA = await fs.stat(a);
const statB = await fs.stat(b);
return statA.mtime.getTime() - statB.mtime.getTime();
});
const resultPath = pathModule.join(path, "docs", "combined.pdf");
const pdfBytes = await imagesToPdf(
sortedSplitFiles,
path,
resultPath,
options.saveToFile
);
// Depending on user's preference, return buffer or path
if (options.saveToFile) {
return resultPath; // Return the path where the file is saved
} else {
return pdfBytes; // Return the buffer directly
}
} catch (err) {
console.error("Error: ", err);
throw err;
} finally {
// Cleanup, if not saving to file
if (!options.saveToFile) {
rimraf.sync(path);
}
}
}
}
async function imagesToPdf(paths, directoryPath, resultPath, saveToFile) {
if (!Array.isArray(paths) || paths.length === 0) {
throw new Error("Must have at least one path in array");
}
const pdfDoc = await PDFDocument.create();
for (const path of paths) {
const imageBytes = await fs.readFile(path);
let image;
if (path.endsWith(".png")) {
image = await pdfDoc.embedPng(imageBytes);
} else if (path.endsWith(".jpg") || path.endsWith(".jpeg")) {
image = await pdfDoc.embedJpg(imageBytes);
} else {
throw new Error("Unsupported image format");
}
const { width, height } = image;
pdfDoc
.addPage([width, height])
.drawImage(image, { x: 0, y: 0, width, height });
}
const pdfBytes = await pdfDoc.save();
if (saveToFile) {
await fs.writeFile(resultPath, pdfBytes);
}
// Return PDF bytes directly unless saving to file
return pdfBytes;
}
module.exports = new Flattener();