猿问

将 HTML 转换为 PDF 发票

我正在尝试生成将转换为 PDF(使用 wkhtmltopdf)并打印在预印信纸上的 HTML 页面(C# Razor View)。

我的问题是,预印的文具有一个标题(简单)和底部(4 厘米)的可撕部分,在打印一些附加信息的最后一页之前应该是空白的。

整个发票正在生成为

<table><thead></thead><tbody></tbody></table>

那么如何在除最后打印的页面之外的每一页上设置 X 的边距,并在最后打印的页面上而不是边距实际打印一些东西?

尝试与 last-child 选择器一起使用,但这不起作用。

也许其他一些解决方案更适合使用 .Net Core(也可以使用一些 linux 应用程序)?


蝴蝶刀刀
浏览 414回答 2
2回答

肥皂起泡泡

终于我明白了。由于页面顶部应该没有问题(也可以用同样的方式处理,但表格顶部是另一种选择)我的解决办法是:&nbsp;wkhtmltopdf test.html --footer-html footer.html output.pdf整个技巧是使用 footer.html 文件:<!DOCTYPE html><html><head>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; var mainHeader = "test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>";&nbsp; &nbsp; &nbsp; &nbsp; var secondHeader = "OOOOOOOOOOPOPOPOPOPOPOPOPOPOPOPOPOPOPOPOPOPOPOPPOPO<br>";&nbsp; &nbsp; &nbsp; &nbsp; function selectHeader() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var vars = {};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var x = document.location.search.substring(1).split('&');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var i in x) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var z = x[i].split('=', 2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vars[z[0]] = decodeURIComponent(z[1]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (vars["page"] == vars['topage']) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('main').innerHTML = secondHeader;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('main').innerHTML = mainHeader;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (vars["page"] == vars['frompage']) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('test').innerHTML = secondHeader;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </script></head><body onload="selectHeader()">&nbsp; &nbsp; <div style="min-height: 6cm; background-color: aqua; max-height: 6cm; overflow:visible;">&nbsp; &nbsp; &nbsp; &nbsp; <div id="main" onload="selectHeader()">&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div></body></html>附:在没有 !DOCTYPE html 的地方阅读它可能无法工作。感谢所有的帮助

一只甜甜圈

您可以使用 CSS@page规则来指定页边距。您还可以使用它来指定左侧和右侧页面(以考虑装订)和第一页的附加边距,但似乎不是最后一页!/* Default left & right is 2cm, top & bottom margin is 4cm */@page { margin: 4cm 2cm }&nbsp;/* First page, 10 cm margin on top */@page :first {&nbsp; margin-top: 10cm;}/* Left pages, a wider margin on the left */@page :left {&nbsp; margin-left: 3cm;&nbsp; margin-right: 2cm;}/* Right pages, a wider margin on the right */@page :right {&nbsp; margin-left: 2cm;&nbsp; margin-right: 3cm;}进一步阅读:https : //www.w3.org/TR/CSS21/page.html#page-box
随时随地看视频慕课网APP
我要回答