Skip to content

Instantly share code, notes, and snippets.

@colinrotherham
Last active August 15, 2019 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colinrotherham/0c49caedf6288b402053a34082d0fafd to your computer and use it in GitHub Desktop.
Save colinrotherham/0c49caedf6288b402053a34082d0fafd to your computer and use it in GitHub Desktop.
Save to PDF from Puppeteer
import puppeteer from 'puppeteer';
(async () => {
const browser = await puppeteer.launch();
// Open new page
const page = await browser.newPage();
// Go to website
await page.goto('http://www.red-phoenix-consulting.co.uk/', {
printBackground: true,
waitUntil: 'networkidle2',
});
// Save to PDF
await page.pdf({
path: 'home.pdf',
format: 'A4',
});
// Close browser
await browser.close();
})();
@colinrotherham
Copy link
Author

To run:

npm install puppeteer
node --experimental-modules save-to-pdf.mjs

Once Node.js 12 LTS is out, we can remove the optional --experimental-modules flag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment