PDF文件是使用空白标题生成的。我应该怎么做才能使用标题图像生成PDF。 当我多次点击节点命令时。经过很长一段时间,它正在生成完整的PDF。 如何使用图像创建HTML文件到PDF。使用木偶戏。
const puppeteer = require('puppeteer');
/* const minimalcss = require('minimalcss');*/
var csstree = require('css-tree');
(async() => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('http://fabalacritas.com/report/report.html?id=85', { waitUntil: 'networkidle2' });
await page.emulateMedia('screen');
await page.waitFor(30000);
await page.pdf({ path: './test.pdf', format: 'Letter', printBackground: true, });
//const IMAGE_LOADING_TIMEOUT = 60 * 1000 * 5;
await browser.close();
})();
依赖关系:
"dependencies": {
"body-parser": "^1.18.2",
"css-tree": "^1.0.0-alpha.27",
"csstree": "0.0.3",
"express": "^4.16.2",
"express-handlebars": "^3.0.0",
"minimalcss": "^0.7.0",
"nodemailer": "^4.4.2",
"pdfkit": "^0.8.3",
"phantom": "^4.0.12",
"puppeteer": "^1.0.0",
"wkhtmltopdf": "^0.3.4"
}
答案 0 :(得分:0)
我鼓励避免超时await page.waitFor(30000);
,这对大型测试案例的影响表现可能是5秒或30秒。
首先使用try-puppeteer网站,以便查看此代码是否适合您(只需复制粘贴):
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('http://fabalacritas.com/report/report.html?id=85', { waitUntil: ['load', 'networkidle2'] });
await page.emulateMedia('screen');
await page.pdf({ path: './test.pdf', format: 'Letter', printBackground: true, });
await browser.close();
请注意,我删除了等待时间,并在goto函数{ waitUntil: ['load', 'networkidle2'] }
中添加了一个新选项,这将设置等待完整加载站点。注意:检查您是否确实需要networkidle2。
现在,这就是您的代码看起来像我的更改的方式:
const puppeteer = require('puppeteer');
/* const minimalcss = require('minimalcss');*/
var csstree = require('css-tree');
(async() => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('http://fabalacritas.com/report/report.html?id=85', { waitUntil: ['load', 'networkidle2'] });
await page.emulateMedia('screen');
await page.pdf({ path: './test.pdf', format: 'Letter', printBackground: true, });
//const IMAGE_LOADING_TIMEOUT = 60 * 1000 * 5;
await browser.close();
})();