Skip to content

Instantly share code, notes, and snippets.

@cheephardware
Last active March 14, 2023 22:38
Show Gist options
  • Save cheephardware/93c30546e9744b892709f735a22a8969 to your computer and use it in GitHub Desktop.
Save cheephardware/93c30546e9744b892709f735a22a8969 to your computer and use it in GitHub Desktop.
Simple local HTML ---> PDF Example using Node.js / Puppeteer.js
//Full PDF options available at: https://pptr.dev/api/puppeteer.pdfoptions
const puppeteer = require('puppeteer');
(async () => {
// Create a browser instance
const browser = await puppeteer.launch();
// Create a new page
const page = await browser.newPage();
// Website URL to export as pdf
const website_url = `file://${__dirname}/hello.html`;
// Open URL in current page
await page.goto(website_url, { waitUntil: 'networkidle0' });
//To reflect CSS used for screens instead of print
await page.emulateMediaType('screen');
// Downlaod the PDF
const pdf = await page.pdf({
path: 'result.pdf',
//margin: { top: '100px', right: '50px', bottom: '100px', left: '50px' },
printBackground: true,
width: '8.5 in',
height: '8.5 in'
});
// Close the browser instance
await browser.close();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment