node.js - Puppeteer - PDFPrint 但 pdf 为空白
问题描述
我对 puppeteer 和 pdf 打印有疑问。
一切看起来都很好,没有错误代码,...但是 pdf 是空白的。和 templateHtml 很好!也许这是函数调用中的错误 await ?
请帮我 !
async function createPDF(quotation, quoteurl, quotname){
var templateHtml = fs.readFileSync(path.join(process.cwd(), './templates/quotation2.html'), 'utf8');
//console.log(templateHtml);
var template = handlebars.compile(templateHtml);
//console.log(template);
var html = template(quotation);
//console.log(html);
var pdfPath = path.join(`${quoteurl}`, `${quotname}.pdf`);
console.log(pdfPath)
var options = {
width: '1230px',
headerTemplate: "<p></p>",
footerTemplate: "<p></p>",
displayHeaderFooter: false,
margin: {
top: "10px",
bottom: "30px"
},
printBackground: true,
path: pdfPath
}
const browser = await puppeteer.launch({
//args: ['--no-sandbox'],
headless: true
});
var page = await browser.newPage();
console.log('OH YES');
await page.goto(`data:text/html;charset=UTF-8,${html}`,{ waitUntil: ['domcontentloaded', 'load'] }).then(function (response) {
// page.emulateMedia('screen')
page.pdf({ path: pdfPath
, format: 'letter' })
.then(function (res) {
browser.close();
}).catch(function (e) {
browser.close();
})
})
}
我不明白为什么它不起作用。
解决方案
我改变了这个内容,它的工作原理!但是 logo.png 没有出现在 pdf 中(或者它与 html 文件位于同一文件夹中。
有什么东西可以添加到 puppeteer 以在 pdf 中包含图像文件吗?(它在 html 文件中声明)
await page.setContent(html,{ waitUntil: ['domcontentloaded', 'load', "networkidle0"] }).then(function (response) {
// page.emulateMedia('screen')
page.pdf({ path: pdfPath,
format: 'A4',
printBackground: true,
margin: {
top: '20px',
bottom: '20px',
right: '20px',
left: '20px' }})
.then(function (res) {
browser.close();
}).catch(function (e) {
browser.close();
})
})
推荐阅读
- python-3.x - 使用 tkinter 从条目小部件将输入打印到文本框
- c++ - 提升精神 x3 错误处理与预期
- excel - 如何修复此 Excel VBA 代码以导入空单元格
- ada - (Ada 2012)编译时错误“预期私有类型...找到复合类型”
- sql - 替换 0 个或多个 html 标记内的文本
- c# - 如何在域/iis 站点中有两个不同的 asp.net 项目
- exc-bad-instruction - *.XLS 文件的 VBA 复制单元格值合并
- docker - docker容器中意外的未映射端口80
- reference - 将 &T 的迭代器收集到 T 集合中的惯用方法是什么?
- drools - 我在哪里可以查看模式是否在规则中失败?