Created
June 2, 2023 19:33
-
-
Save rossta/4f01f56e43ceca86bd455f7a2dfa5156 to your computer and use it in GitHub Desktop.
Generate PDF from webpage with puppeteer
This file contains hidden or 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
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