-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeabook.js
More file actions
133 lines (100 loc) · 3.09 KB
/
makeabook.js
File metadata and controls
133 lines (100 loc) · 3.09 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { PageSizes, PDFDocument, degrees } from 'pdf-lib'
import * as fs from 'fs';
function createPageArrays(max) {
let lefts = [];
let rights = [];
let half = max/2;
for(let i = 0; i < half; i++){
if(i%2){ //backsides of pages
lefts.push(i);
rights.push(max - 1 - i);
}
else{ //frontsides of pages
lefts.push(max - 1 - i);
rights.push(i);
}
}
return {lefts, rights};
}
function measurePage(page, margin = 10){
const { width, height } = page.getSize();
const scalevar = (width/595.28 < height/841.89) ? width : height;
const scale = 420.9/(scalevar + 2*margin);
const x = 595.28 - margin;
const lefty = margin;
const righty = 420.9 + margin;
return {x, lefty, righty, scale}
}
async function copyPages(fileName) {
const firstDonorPdfBytes = fs.readFileSync(fileName);
const firstDonorPdfDoc = await PDFDocument.load(firstDonorPdfBytes);
let numPages = firstDonorPdfDoc.getPageCount()
if(!(numPages % 2)) {
firstDonorPdfDoc.addPage().drawText('bweh');
numPages += 1;
}
var lefts, rights;
const pageArrays = createPageArrays(numPages);
lefts = pageArrays.lefts;
rights = pageArrays.rights;
const pages = firstDonorPdfDoc.getPages();
const leftPages = [];
const rightPages = [];
lefts.forEach(element => {
leftPages.push(pages[element]);
});
rights.forEach(element => {
rightPages.push(pages[element]);
});
const mergedPdf = await PDFDocument.create();
const embeddedLeft = await mergedPdf.embedPages(leftPages);
const embeddedRight = await mergedPdf.embedPages(rightPages);
/*
Before we get to arranging the pages, let's talk the coordinate system.
X and Y axes are based on the un-rotated page, and start at bottom left.
After rotation, the origin is at the top right.
Pages are anchored at their bottom left, which is also top right when anchored.
A4 size: [595.28, 841.89]
*/
embeddedLeft.forEach((element, i) => {
let dims = measurePage(leftPages[i]);
let page = mergedPdf.addPage(PageSizes.A4);
page.drawPage(element, {
x: dims.x, //maxwidth
y: dims.lefty,
xScale: dims.scale,
yScale: dims.scale,
rotate: degrees(90)
})
});
embeddedRight.forEach((element, i) => {
let dims = measurePage(rightPages[i]);
let page = mergedPdf.getPage(i);
page.drawPage(element, {
x: dims.x,
y: dims.righty,
xScale: dims.scale,
yScale: dims.scale,
rotate: degrees(90)
})
});
return mergedPdf;
// Insert the second copied page to index 0, so it will be the
// first page in `pdfDoc`
//pdfDoc.insertPage(0, secondDonorPage)
// Serialize the PDFDocument to bytes (a Uint8Array)
//const pdfBytes = await pdfDoc.save()
// For example, `pdfBytes` can be:
// • Written to a file in Node
// • Downloaded from the browser
// • Rendered in an <iframe>
}
async function runPrinter(inFile, outFile){
const pdf = await copyPages(inFile);
const pdfBytes = await pdf.save();
fs.writeFileSync(outFile, pdfBytes);
console.log(`PDF file written to: ${outFile}`);
}
runPrinter('input filename',
'output filename'
);