forked from vladotg/puppeteer-pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
56 lines (48 loc) · 1.4 KB
/
main.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
#!/usr/bin/env node
import fileUrl from "file-url";
import isUrl from "is-url";
import puppeteer from "puppeteer";
import cli from "./src/cli";
import { prepareOptions } from "./src/options";
import fixBrokenPdf from "./src/fixBrokenPdf";
(async () => {
const cliOptions = cli.opts();
const options = prepareOptions(cliOptions);
if (options.brokenPdf !== undefined && options.fixedPdf !== undefined) {
fixBrokenPdf(options);
return;
}
const executablePath = process.env.CHROME_BIN || "/usr/bin/google-chrome-stable";
const browser = await puppeteer.launch({
headless: 'new',
args: [
"--no-sandbox",
"--no-zygote",
"--disable-gpu"
],
executablePath
});
const page = await browser.newPage();
try {
await page.setViewport({width: 1240, height: 1448, deviceScaleFactor: options.deviceScaleFactor || 1});
// Get URL / file path from first argument
const location = cli.args[0];
await page.goto(isUrl(location) ? location : fileUrl(location), {
waitUntil: options.waitUntil || "networkidle2"
});
// Output options if in debug mode
if (options.debug) {
console.log(options);
}
await page.pdf(options);
await browser.close();
} catch(e) {
console.error(e);
} finally {
// Kill group on error.
const pid = -browser.process().pid;
try {
process.kill(pid, 'SIGKILL');
} catch (e) {}
}
})();