Created
June 13, 2022 14:19
-
-
Save jaredpalmer/c16198902839267271b30e3bf68de658 to your computer and use it in GitHub Desktop.
This file contains 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' | |
import fs from 'fs' | |
async function buildPDF(htmlString) { | |
const browser = await puppeteer.launch({ headless: true }) | |
const page = await browser.newPage(); | |
await page.setContent(htmlString, { waitUntil: 'networkidle0' }) | |
const pdf = await page.pdf({ | |
format: 'A4', | |
displayHeaderFooter: false, | |
printBackground: true, | |
// default in Chrome | |
margin: { | |
top: '0.4in', | |
bottom: '0.4in', | |
left: '0.4in', | |
right: '0.4in', | |
} | |
}) | |
await browser.close() | |
fs.writeFileSync('/path/to/save', pdf) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment