admin管理员组文章数量:1404924
My Puppeteer version is v24.4.0.
const imgBase64 = readFileSync(imgPath).toString("base64");
const headerTemplate = `
<div style="width:100%; height: 50px;">
<img src="data:image/png;base64,${imgBase64}" alt="logo">
</div>`;
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.setContent(htmlContent, { waitUntil: "networkidle0" });
await page.pdf({
path: outputPath,
format: "A4",
displayHeaderFooter: true,
margin: { top: "390px", bottom: "150px", left: "20px", right: "20px" },
headerTemplate: headerTemplate,
footerTemplate: footerTemplate,
printBackground: true,
});
await browser.close();
This is the Node.js code for generating PDF involving image in header. But it occurs in error like this:
ProtocolError: Protocol error (Page.printToPDF): Printing failed
How to solve this issue?
My Puppeteer version is v24.4.0.
const imgBase64 = readFileSync(imgPath).toString("base64");
const headerTemplate = `
<div style="width:100%; height: 50px;">
<img src="data:image/png;base64,${imgBase64}" alt="logo">
</div>`;
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.setContent(htmlContent, { waitUntil: "networkidle0" });
await page.pdf({
path: outputPath,
format: "A4",
displayHeaderFooter: true,
margin: { top: "390px", bottom: "150px", left: "20px", right: "20px" },
headerTemplate: headerTemplate,
footerTemplate: footerTemplate,
printBackground: true,
});
await browser.close();
This is the Node.js code for generating PDF involving image in header. But it occurs in error like this:
ProtocolError: Protocol error (Page.printToPDF): Printing failed
How to solve this issue?
Share Improve this question edited Mar 10 at 3:52 ggorlen 58k8 gold badges114 silver badges157 bronze badges asked Mar 9 at 3:31 kevin Escalantekevin Escalante 111 silver badge5 bronze badges 1- Related: [Bug]: Error when parsing page to PDF: ProtocolError: Protocol error (Page.printToPDF): Printing failed and Puppeteer | Protocol error (Page.printToPDF): Printing failed – ggorlen Commented Mar 10 at 4:01
1 Answer
Reset to default 0I do not have a good answer for this, but I can tell you that it seems like the assets in the header/footer need to be in the body as well, otherwise the PDF fails to render. I am working through this issue currently.
Adding the same image tag to the base htmlContent
but with a display;none
allows it to render properly.
版权声明:本文标题:javascript - How to load a header template image when generating PDF using Puppeteer in Node.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744879243a2630105.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论