forked from namespace-ee/puppeteer-pdf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpuppeteer-pdf.js
executable file
·130 lines (119 loc) · 3.68 KB
/
puppeteer-pdf.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
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
#!/usr/bin/env node
const _ = require("lodash");
const cli = require("commander");
const fileUrl = require("file-url");
const fs = require("fs");
const isUrl = require("is-url");
const puppeteer = require("puppeteer");
cli
.version("1.2.0")
.option("-p, --path <path>", "The file path to save the PDF to.")
.option(
"-s, --scale [scale]",
"Scale of the webpage rendering.",
parseFloat,
1
)
.option("-dhf, --displayHeaderFooter", "Display header and footer.", false)
.option(
"-ht, --headerTemplate [template]",
"HTML template for the print header."
)
.option(
"-ft, --footerTemplate [template]",
"HTML template for the print footer."
)
.option("-pb, --printBackground", "Print background graphics.", false)
.option("-l, --landscape", "Paper orientation.", false)
.option(
"-pr, --pageRanges <range>",
"Paper ranges to print, e.g., '1-5, 8, 11-13'. Defaults to the empty string, which means print all pages."
)
.option(
"-f, --format [format]",
"Paper format. If set, takes priority over width or height options. Defaults to 'Letter'.",
"Letter"
)
.option(
"-w, --width [width]",
"Paper width, accepts values labeled with units."
)
.option(
"-h, --heigh [height]",
"Paper height, accepts values labeled with units."
)
.option(
"-mt, --marginTop [margin]",
"Top margin, accepts values labeled with units."
)
.option(
"-mr, --marginRight [margin]",
"Right margin, accepts values labeled with units."
)
.option(
"-mb, --marginBottom [margin]",
"Bottom margin, accepts values labeled with units."
)
.option(
"-ml, --marginLeft [margin]",
"Left margin, accepts values labeled with units."
)
.option("-d, --debug", "Output Puppeteer PDF options")
.option(
"-wu, --waitUntil [choice]",
"waitUntil accepts choices load, domcontentloaded, networkidle0, networkidle2. Defaults to 'networkidle2'.",
"networkidle2"
)
.option("-stb, --setTransparentBackground", "Set transparent background.", false)
.action(function(required, optional) {
// TODO: Implement required arguments validation
})
.parse(process.argv);
(async () => {
let options = {};
// Loop through options
_.each(cli.options, function(option) {
const optionName = option.name();
if (!_.isNil(cli[optionName]) && !["version"].includes(optionName)) {
const optionValue = cli[optionName];
if (_.startsWith(optionName, "margin")) {
// Margins need to be combined into an object
_.set(
options,
["margin", optionName.replace("margin", "").toLowerCase()],
optionValue
);
} else {
_.set(options, optionName, optionValue);
}
}
});
// Check if we need to read header or footer templates from files
_.each(["headerTemplate", "footerTemplate"], function(template) {
if (_.get(options, template, "").startsWith("file://")) {
options[template] = fs.readFileSync(
options[template].replace("file://", ""),
"utf-8"
);
}
});
const browser = await puppeteer.launch({ args: ["--no-sandbox"] });
const page = await browser.newPage();
// Get URL / file path from first argument
const location = _.first(cli.args);
await page.goto(isUrl(location) ? location : fileUrl(location), {
waitUntil: _.get(options, "waitUntil", "networkidle2")
});
// Output options if in debug mode
if (cli.debug) {
console.log(options);
}
if (cli.setTransparentBackground) {
await page._emulationManager._client.send(
'Emulation.setDefaultBackgroundColorOverride',
{ color: { r: 0, g: 0, b: 0, a: 0 } }
);
}
await page.pdf(options);
await browser.close();
})();