gpt4 book ai didi

javascript - 在 PDF 创建的页脚内打印变量

转载 作者:行者123 更新时间:2023-12-03 00:47:24 26 4
gpt4 key购买 nike

我需要在页面页脚内打印一个对象字段。目前我有:

async function createPdf(obj, res) {
//other code
const pdf = await page.pdf({
format: 'A4',
printBackground: true,
displayHeaderFooter : true,
headerTemplate : "",
footerTemplate : "<style>.footer{width: 100%; font-size:12px; text-align:right; color:white; background:#161719; -webkit-print-color-adjust:exact; margin-bottom:-15px;}</style><div class='footer'><span>page </span><span class='pageNumber'></span>/<span class='totalPages'></span></div>",
//margin top 0 removes header
margin: {top: "00", bottom: "18"}
});
//other code
}

我需要在页脚内打印一些 obj 过滤器,但我尝试过的所有方法,它都将 obj 打印为字符串而不是内容。我的目标有可能实现吗?

最佳答案

为了在使用 page.pdf() 创建 PDF 时打印页脚内部的变量,您可以将 footerTemplate HTML 放置在 template literal 中(``) 并将变量插入占位符 (${}) 内。

请参阅下面的示例:

const example = 'Hello, world!';

const pdf = await page.pdf({
format: 'A4',
printBackground: true,
displayHeaderFooter: true,
headerTemplate: '',
footerTemplate: `<style>
.footer {
background: #161719;
color: #fff;
-webkit-print-color-adjust: exact;
color-adjust: exact;
font-size: 12px;
margin-bottom: -15px;
text-align: right;
width: 100%;
}
</style>
<div class="footer">
<span>${example}</span> <!-- Variable Inside Footer -->
<span>page</span>
<span class="pageNumber"></span>/<span class="totalPages"></span>
</div>`,
//margin top 0 removes header
margin: {
top: 0,
bottom: 18,
},
});

关于javascript - 在 PDF 创建的页脚内打印变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53186520/

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