使用 html css 将页脚添加到 pdf 的每一页
我正在使用nodejs中的ejs文件生成PDF。是否可以在生成的 PDF 的每一页上添加页脚?
这是我的 ejs 文件:
<!DOCTYPE html>
<html>
...
<head>
<style>
footer{
background-color: red;
width: 100%;
position: absolute;
bottom: 0;
left: 0;
right: 0;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
</style>
</head>
<body> ... </body>
<footer>
<p>this is footer</p>
</footer>
</html>
这是我的 nodejs 代码片段:
ejs.renderFile(path.join(__dirname, './views', "report-template.ejs"), { invoicedata: JSON.parse(results[0].jsondata), moment:moment }, (fileerr, data) => {
if (fileerr) {
console.log("Error!", fileerr);
} else {
let options = {
"height": "11.7in",
"width": "8.3in",
}
pdf.create(data, options).toFile("report.pdf", function (err, data) {
if (err) {
console.log("error!!");
} else {
console.log("file created successfully");
}
})
}
})
输出的 pdf 得到的是:
I am generating PDF using ejs file in nodejs. Is it possible to add Footer on every page of the generated PDF?
Here is my ejs file:
<!DOCTYPE html>
<html>
...
<head>
<style>
footer{
background-color: red;
width: 100%;
position: absolute;
bottom: 0;
left: 0;
right: 0;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
</style>
</head>
<body> ... </body>
<footer>
<p>this is footer</p>
</footer>
</html>
here is my nodejs code snippet:
ejs.renderFile(path.join(__dirname, './views', "report-template.ejs"), { invoicedata: JSON.parse(results[0].jsondata), moment:moment }, (fileerr, data) => {
if (fileerr) {
console.log("Error!", fileerr);
} else {
let options = {
"height": "11.7in",
"width": "8.3in",
}
pdf.create(data, options).toFile("report.pdf", function (err, data) {
if (err) {
console.log("error!!");
} else {
console.log("file created successfully");
}
})
}
})
The output pdf getting is:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然在 HTML 中模拟分页符页眉和页脚相当容易,但对于专为不受限制的页面宽度或长度而设计的格式来说,它们并不自然。
通常将 HTML 对象描述为可变画布的百分比,转换为 PDF 所需的固定笛卡尔页面很容易出现舍入错误。
此处作为示例,主要目标是在 Microsoft Edge(基于 Chrome)中模拟 PDF 布局,但在一个浏览器中非常适合查看和打印的小提琴因素将需要在另一个浏览器中进行调整。
请参阅插图,其中完全相同的代码和目标打印机的页面在边缘打印预览中非常完美,但在 Internet Explore 中,在相同的打印机默认设置上会出现蠕变!因此,该垃圾页面通常需要进行一些细微的调整才能保持同步,从而导致源代码的两个不同副本!
Whilst it is fairly easy to emulate page breaks headers and footers in HTML they are not natural for a format that is designed for unfettered page widths or lengths.
It is common to describe HTML objects as % of a variable canvas the conversion to the fixed Cartesian pages needed by PDF is prone to rounding errors.
Here as an example, the primary aim was to emulate a PDF layout in Microsoft Edge (Chrome based) but the fiddle factors that work fairly well for view and print in one browser will need adjustments in another.
SEE the inset where the page for exactly the same code and target printer which is perfect in edge print preview, is subject to creep-age in Internet Explore, on the same printer defaults !! That crap-page thus often requires minor tweaking to keep it in sync, leading to two different copies of source !
也许这个 CSS 可以提供帮助?即,将“绝对”位置更改为固定?
这个问题中有一些评论可能会有所帮助:
如何使用 HTML 在文档的每个打印页上打印页眉和页脚?
Maybe this CSS can help? I.e. change "absolute" position to fixed?
There were some comments in this question that might be helpful:
How to use HTML to print header and footer on every printed page of a document?
我们有同样的问题。我们找到了带有背景图像的解决方案。只需创建 A4 尺寸/像素的背景图像(您可以在谷歌上找到)。并尝试设置背景图片。希望它能起作用。祝一切顺利,谢谢。
We have same issue. We found a solution with background image. Just create background image of A4 size / pixel (You can find on google). And try to set background image. Hope it will works. All the best thanks.