Skip to content

Instantly share code, notes, and snippets.

@pablosbrain
Last active September 1, 2020 08:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pablosbrain/434e1478b404845a6e1c5ba2472f4157 to your computer and use it in GitHub Desktop.
Save pablosbrain/434e1478b404845a6e1c5ba2472f4157 to your computer and use it in GitHub Desktop.
Simple Node.js Puppeteer PDF Output Example
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