-
Notifications
You must be signed in to change notification settings - Fork 0
/
print.js
87 lines (69 loc) · 2.56 KB
/
print.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
const puppeteer = require('puppeteer')
const program = require('commander')
const { join } = require('path')
const packageJson = require('./package.json')
const defaultPapersPerSections = '6'
const defaultSections = '8'
const PRINT_HTML_FILENAME = 'print.html'
const printHtmlPath = join(__dirname, PRINT_HTML_FILENAME)
function parseArgs() {
program
.version(packageJson.version, '-v, --version')
.option('--papers-per-section <papers>', 'How many sheets of papers does one section have?', defaultPapersPerSections)
.option('--sections <number>', 'How many sections does the book have? A section is a bunch of papers, folded together.', defaultSections)
.parse(process.argv)
return {
papersPerSection: program.papersPerSection,
sections: program.sections,
}
}
async function printAll() {
const config = parseArgs()
let outputFileCounter = 1
for (let section = 0; section < config.sections; section++) {
const lastPageOfSection = (config.papersPerSection * 4)
const pageStart = section * config.papersPerSection * 4
for (let page = 1; page < config.papersPerSection * 2; page += 2) {
const first = page + pageStart
const second = page + pageStart + 1
const secondLast = lastPageOfSection - page + pageStart
const last = lastPageOfSection - page + pageStart + 1
await printPage({
pageNumber: outputFileCounter,
first,
second,
secondLast,
last,
hasLastPage: section === config.sections - 1 && page === 1
})
outputFileCounter++
}
}
}
async function printPage({
pageNumber,
first,
second,
secondLast,
last,
hasLastPage,
}) {
console.log(`print page ${pageNumber}`)
const browser = await puppeteer.launch({
headless: true,
args: ['--disable-web-security', '--user-data-dir=chrome-user']
});
const query = `first=${first}&second=${second}&secondLast=${secondLast}&last=${last}&hasLastPage=${hasLastPage}`
const page = await browser.newPage();
await page.goto(`file://${printHtmlPath}?${query}`, { waitUntil: 'networkidle0' });
await page.pdf({
format: 'A4',
path: `${__dirname}/output-${pageNumber.toString().padStart(2, '0')}.pdf`,
landscape: true,
displayHeaderFooter: false,
printBackground: true,
})
await browser.close()
console.log(`page ${pageNumber} successfully printed!`)
}
printAll()