gpt4 book ai didi

本地 html 的 puppeteer css 未显示

转载 作者:行者123 更新时间:2023-12-05 00:39:54 24 4
gpt4 key购买 nike

我正在尝试使用本地 html 生成 pdf,但 css 没有显示出来

 (async () => {
var html = '<div style="width:500px; height:500px;background:blue">test</div>'
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent(html)
await page.pdf({path: 'hn.pdf', format: 'A4'});

await browser.close();
})();

我怎样才能让 css 工作?

最佳答案

printBackground: true 添加到您的 page.pdf 调用

await page.pdf({path: 'hn.pdf', format: 'A4', printBackground: true});

问题不在于 CSS,显示了 CSS,但是当您打印页面时,背景会被删除。尝试将 color:red; 添加到您的 CSS 中,您会发现 CSS 可以正常工作。

关于本地 html 的 puppeteer css 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54284489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com