-
Notifications
You must be signed in to change notification settings - Fork 0
/
a4.js
executable file
·101 lines (93 loc) · 2.85 KB
/
a4.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
#!/usr/bin/env node
/**
* Create a PDF using puppeteer
*/
import { existsSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { Command, Option } from "commander";
import puppeteer from "puppeteer-core";
const program = new Command();
const extensions = "html|png|jpg|svg";
const numbering = {
displayHeaderFooter: true,
headerTemplate: `<div />`,
footerTemplate: `<div style="
font-size: 10px;
width: 1000px;
text-align: center;
">
Page <span class="pageNumber"></span> of <span class="totalPages"></span>
</div>`,
};
const paths = ["/usr/bin/chromium-browser", "/opt/google/chrome/chrome"];
let input;
let pdf;
program
.version("0.1.1")
.arguments(`<source.(${extensions})> [destination.pdf]`)
.action((source, destination) => {
input = source;
if (destination) pdf = destination;
else pdf = source.replace(RegExp(`[.](${extensions})$`), ".pdf");
})
.option("-b, --background", "print background graphics")
.addOption(
new Option("-f, --format <format>", "page size")
.default("a4")
.choices(["a0", "a1", "a2", "a3", "a4", "a5", "a6", "letter"]),
)
.option("-l, --landscape", "landscape orientation")
.option("-n, --number", "number pages")
.parse();
/*
* Options listed at:
* https://pptr.dev/api/puppeteer.pdfoptions
*/
let flagged = false;
const exit = (message) => {
console.error(message);
flagged = true;
};
const http = RegExp(`^http`).test(input);
if (!http) {
if (!RegExp(`.(?:${extensions})$`).test(input)) {
exit(`${input} doesn't end with ${extensions}`);
}
if (!existsSync(input)) exit(`${input} doesn't exist`);
}
if (!/\.pdf$/.test(pdf)) exit(`${pdf} doesn't end with .pdf`);
if (!existsSync(dirname(pdf))) exit(`Directory for ${pdf} doesn't exist`);
if (flagged) {
program.outputHelp();
process.exit(1);
}
(async () => {
const options = program.opts();
const browser = await puppeteer.launch({
executablePath: paths.filter(existsSync)[0],
});
const page = await browser.newPage();
const url = http ? `${input}` : `file:///${resolve(input)}`;
await page.goto(url, { waitUntil: "networkidle2" });
console.log(pdf);
await page.pdf({
...{
path: pdf,
format: options.format,
landscape: options.landscape || false,
/* margins set to 25mm to match Microsoft Word Online defaults */
margin: { top: "25mm", right: "25mm", bottom: "25mm", left: "25mm" },
printBackground: options.background || false,
},
...(options.number ? numbering : {}),
});
await browser.close();
})();
// a4.js
//
// This Source Code Form is subject to the terms of the Mozilla Public License,
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at https://mozilla.org/MPL/2.0/.
//
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2019 Keith Maxwell <keith.maxwell@gmail.com>