Last active
September 1, 2020 08:49
-
-
Save pablosbrain/434e1478b404845a6e1c5ba2472f4157 to your computer and use it in GitHub Desktop.
Simple Node.js Puppeteer PDF Output Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const puppeteer = require('puppeteer'); | |
const path = require('path'); | |
var browser, page; | |
(async () => { | |
browser = await puppeteer.launch({ | |
args: ['--no-sandbox', '--disable-setuid-sandbox'] | |
, headless: true // printo to pdf only works in headless mode currently | |
}); | |
page = await browser.newPage(); | |
// this section can loop for processing of multiple files if needed. | |
var pagePath = 'file:///C:/test.html'; | |
var thispage = await page.goto(pagePath, { waitUntil: 'networkidle' }); | |
await page.waitFor(300); | |
await page.emulateMedia('print'); | |
await page.pdf({ | |
path: 'test.pdf' | |
, format: 'A4' | |
, printBackground: true | |
, landscape: true | |
, margin: { top: "0", right: "0", bottom: "0", left: "0" } | |
}); | |
await browser.close(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment