一、 服务器中安装 puppeteer
npm install puppeteer -g
二、服务器安装相对应软件
环境 CenteOs7.2
# 依赖库yum installpango.x86_64libXcomposite.x86_64libXcursor.x86_64libXdamage.x86_64libXext.x86_64libXi.x86_64libXtst.x86_64cups-libs.x86_64libXScrnSaver.x86_64libXrandr.x86_64GConf2.x86_64alsa-lib.x86_64atk.x86_64gtk3.x86_64 -y
# 字体yum installipa-gothic-fontsxorg-x11-fonts-100dpixorg-x11-fonts-75dpixorg-x11-utilsxorg-x11-fonts-cyrillicxorg-x11-fonts-Type1xorg-x11-fonts-misc -y
三、执行demo
// Egg.js项目 - /service/exportService.jsimport { Service } from 'egg';import puppeteer = require('puppeteer');export default class ExportService extends Service {public async exportPdf() {const browser = await puppeteer.launch({args: [ '--disable-dev-shm-usage', '--no-sandbox' ],});const page = await browser.newPage();await page.goto('http://www.baidu.com');// 页脚const footerTemplate = `<divstyle="width:80%;margin:0 auto;font-size:8px;border-top:1px solid #ddd;padding:10px 0;display: flex; justify-content: space-between; "><span style="">我是页脚</span><div><span class="pageNumber"></span> / <span class="totalPages"></span></div></div>`;const options: puppeteer.PDFOptions = {format: 'a4',printBackground: true,displayHeaderFooter: true,footerTemplate,};// 封面const page1 = await page.pdf({...options,});const pdfs = page1;return pdfs;}}
