-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
74 lines (70 loc) · 1.81 KB
/
test.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
const fs = require("fs");
const sharp = require("sharp");
const PDF = require("./index");
if (!fs.existsSync("./output")) fs.mkdirSync("./output");
/**
* Export images from a PDF file
*/
PDF.sharpsFromPdf("./input.pdf", {
sharpOptions: {},
delay: -1,
workerSrc: false,
handler(event, data) {
if (event === "loading") {
console.log("loading PDF:", (data.loaded / data.total) * 100);
} else if (event === "loaded") {
console.log("PDF loaded");
} else if (event === "image" || event === "skip" || event === "error") {
console.log("parsing images:", (data.pageIndex / data.pages) * 100);
} else if (event === "done") {
console.log("done");
}
},
}).then((images) => {
images.forEach(({ image, channels, name }) => {
const ext = channels > 3 ? ".png" : ".jpg";
image.toFile(`./output/${name}${ext}`);
});
});
/**
* Generate a PDF file from images
*/
PDF.sharpsToPdf(
[
{
image: sharp("./image1.png"),
options: { fit: false, handler() {} },
},
sharp("./image2.jpg"),
],
"./output/output.pdf",
// { type: "arraybuffer" },
{
pdfOptions: {
format: "b5",
// encryption: {
// userPassword: "ssnangua",
// },
},
imageOptions: {
fit: true,
margin: 20,
handler({ doc, ...params }) {
const { imageData, format, x, y, width, height } = params;
doc.addImage(imageData, format, x, y, width, height);
const { index, pageWidth, pageHeight } = params;
doc.text(`- ${index + 1} -`, pageWidth / 2, pageHeight - 10, {
align: "center",
baseline: "bottom",
});
return false;
},
},
// autoSize: true,
// init({ doc, ...params }) {
// console.log("init", params);
// },
}
).then((data) => {
console.log(data);
});