Skip to content

Instantly share code, notes, and snippets.

@rossta
Created June 2, 2023 19:33
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 rossta/4f01f56e43ceca86bd455f7a2dfa5156 to your computer and use it in GitHub Desktop.
Save rossta/4f01f56e43ceca86bd455f7a2dfa5156 to your computer and use it in GitHub Desktop.
Generate PDF from webpage with puppeteer
import puppeteer from 'puppeteer';
const [, , url, folder = '.'] = process.argv;
if (!url) throw 'Must supply URL';
(async () => {
const browser = await puppeteer.launch({ headless: 'new' });
const page = await browser.newPage();
await page.goto(url, { waitUntil: 'networkidle0' });
// Set screen size
// await page.setViewport({ width: 1080, height: 1024 });
// await page.emulateMediaType('screen');
const path = `${folder}/${encodeURIComponent(url)}.pdf`;
console.log(`Generating pdf at ${path}`);
const pdf = await page.pdf({
path,
format: 'A4',
printBackground: true,
margin: true,
});
await browser.close();
return pdf;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment