-
Notifications
You must be signed in to change notification settings - Fork 1
/
booklet.js
100 lines (82 loc) · 2.54 KB
/
booklet.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
#!/usr/bin/env node
/* Studio Webux S.E.N.C 2022 */
const pdf = require("pdfjs");
const fs = require("fs");
/**
*
* @param {Number} pageCount Page Count of the PDF
* @param {Number} signature The number of pages per signature
* @returns reordered pages
*/
function processing(pageCount, signature) {
if ((pageCount / signature) % 1 !== 0) throw new Error("Invalid page count");
const pagesPerSignature = pageCount / signature;
let bindings = [];
let pages = [];
for (let x = 1; x <= pageCount / pagesPerSignature; x += 1) {
let section = [];
for (let y = 0; y < pagesPerSignature / 2; y += 1) {
let item = [];
if (y % 2 === 0) {
item = [pagesPerSignature * x - y, pagesPerSignature * (x - 1) + y + 1];
}
if (y % 2 === 1) {
item = [pagesPerSignature * (x - 1) + y + 1, pagesPerSignature * x - y];
}
section.push(item);
pages.push(...item);
}
bindings.push(section);
}
return { pages, bindings };
}
async function pdfHandler(src, signatures, output = "output.pdf") {
const doc = new pdf.Document({
font: require("pdfjs/font/Helvetica"),
padding: 10,
});
const loadedSource = new pdf.ExternalDocument(fs.readFileSync(src));
const document = processing(loadedSource.pageCount, signatures);
doc.pipe(fs.createWriteStream(output));
document.pages.forEach((page) => {
doc.addPageOf(page, loadedSource);
});
await doc.end();
return {
pageCount: document.pages.length,
output,
pages: document.pages,
bindings: document.bindings,
};
}
(async () => {
try {
const argv = process.argv.splice(2);
const src = argv[0];
const signatures = argv[1];
const output = argv[2];
if (!src || !signatures) {
console.error(
"[USAGE] yat-booklet <source.pdf> <signatures count> [output.pdf]"
);
process.exit(2);
}
if (!src || !src.includes(".pdf"))
throw new Error("The 'source' must be a PDF");
if (!signatures || isNaN(signatures))
throw new Error("The 'signatures count' must be a number");
if (output && !output.includes(".pdf"))
throw new Error("The 'output' must be a PDF");
const info = await pdfHandler(src, signatures, output);
console.log(
`Document processed with success !
Saved to '${info.output}'
Summary: '${info.pageCount}' pages split into '${signatures}' signature(s)`
);
if (process.env.PREVIEW === "true" || process.env.PREVIEW === "1")
console.debug(info.bindings);
} catch (e) {
console.error(e.message);
process.exit(1);
}
})();