-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuppeteer.mjs
48 lines (34 loc) · 1.25 KB
/
puppeteer.mjs
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
import puppeteer from 'puppeteer';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
(async () => {
const originalLog = console.log;
// Overwriting
console.log = function () {
var args = [].slice.call(arguments);
originalLog.apply(console.log,[getCurrentDateString()].concat(args));
};
// Returns current timestamp
function getCurrentDateString() {
return (new Date()).toISOString() + ' ::';
};
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
console.log("Loading browser");
const browser = await puppeteer.launch();
const page = await browser.newPage();
page.setJavaScriptEnabled(false);
console.log("Loading document");
// __dirname is a global node variable that corresponds to the absolute
// path of the folder containing the currently executing file
await page.goto(`file://${__dirname}/document.html`, { waitUntil: 'networkidle0', timeout: 0 });
console.log("Creating PDF");
const pdf = await page.pdf({
path: 'puppeteer.pdf',
margin: { top: '100px', right: '50px', bottom: '100px', left: '50px' },
printBackground: true,
format: 'A4',
timeout: 0,
});
await browser.close();
})();