使用 puppeteer 实现 PDF 文件合成

July 03, 2021

1、下面是使用一个 url 的界面生成

const puppeteer = require("puppeteer")

const createPDF = async () => {
  const browser = await puppeteer.launch()
  const page = await browser.newPage()
  await page.goto("https://example.com")
  const pdf = await page.pdf({ format: "A4" })
  await browser.close()
  return pdf
}

2、还可以使用 html 模板

const puppeteer = require("puppeteer")

const createPDF = async () => {
  const browser = await puppeteer.launch()
  const page = await browser.newPage()
  await page.setContent("<h1>hello</h1>")
  const pdf = await page.pdf({ format: "A4" })
  await browser.close()
  return pdf
}

Profile picture

Written by Vance who lives and works in Shenzhen, China, and is working hard to improve. You should follow them on csdn