Skip to content

Instantly share code, notes, and snippets.

@roylee0704
Forked from antfu/html2pdf.js
Created July 5, 2020 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save roylee0704/401b8ea43d20c8b59e08abe715db011e to your computer and use it in GitHub Desktop.
Save roylee0704/401b8ea43d20c8b59e08abe715db011e to your computer and use it in GitHub Desktop.
HTML to PDF
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