英文:
How to Create PDF with recurring logo using iText, HTML and thymeleaf
问题
I am making use of HTML, thymeleaf and flying-saucer-pdf
我正在使用HTML、Thymeleaf和飞翔器PDF。
I have the code at following link to convert html to pdf: https://github.com/Naresh-Chaurasia/html-pdf-generation-1x
我在以下链接中有将HTML转换为PDF的代码:https://github.com/Naresh-Chaurasia/html-pdf-generation-1x
HTML:https://github.com/Naresh-Chaurasia/html-pdf-generation-1x/blob/main/src/main/resources/pdf-templates/order-template.html
Java To Convert HTML to PDF using iText: https://github.com/Naresh-Chaurasia/html-pdf-generation-1x/blob/main/src/main/java/in/connect2tech/html/pdf/HtmlToPdfMain.java
使用iText将HTML转换为PDF的Java代码:https://github.com/Naresh-Chaurasia/html-pdf-generation-1x/blob/main/src/main/java/in/connect2tech/html/pdf/HtmlToPdfMain.java
Everything works fine and below is the sample PDF generated.
一切都运行正常,以下是生成的示例PDF。
Problem Statement: The problem arises when the record spans more than 1 page. I am not able to create the header in all the pages, as can be seen in the screenshot below, the records start without the header and logo.
问题陈述:当记录跨越多于1页时会出现问题。我无法在所有页面上创建标题,如下面的截图所示,记录在没有标题和徽标的情况下开始。
Question:How can I repeat the header logo on all the generated pdfs.
问题:如何在所有生成的PDF中重复标题和徽标。
Thanks for any pointers/help in advance.
在此提前感谢任何指导或帮助。
英文:
I am making use of HTML, thymeleaf and flying-saucer-pdf
I have the code at following link to convert html to pdf: https://github.com/Naresh-Chaurasia/html-pdf-generation-1x
Java To Convert HTML to PDF using iText: https://github.com/Naresh-Chaurasia/html-pdf-generation-1x/blob/main/src/main/java/in/connect2tech/html/pdf/HtmlToPdfMain.java
Everything works fine and below is the sample PDF generated.
Problem Statement: The problem arises when the record spans more than 1 page. I am not able to create the header in all the pages, as can be seen in the screenshot below, the records start without the header and logo.
Question:How can I repeat the header logo on all the generated pdfs.
Thanks for any pointers/help in advance.
答案1
得分: 1
你需要使用CSS 2.0的分页媒体来定义HTML文档的页眉。
- 创建一个包含要作为页眉重复显示的内容的div
<div id="header">
<table>
<tr>
<td>
<img width="80" src="https://simplesolution.dev/images/Logo_S_v1.png" />
</td>
<td>
<h1>供应订单表格</h1>
</td>
</tr>
</table>
<div>
<table>
<tr>
<td th:text="'日期: ' + ${#dates.format(order.date, 'dd/MM/yyyy')}" ></td>
</tr>
(...)
</table>
</div>
</div>
- 添加以下CSS以在每个页面上重复显示此内容
@page {
@top-center { content: element(header); }
margin-top:10cm; /* 根据要重复显示的元素的大小调整此值 */
/* you want to repeat on each page */
}
#header {
position: running(header);
}
你也可能想要重复表格的标题。
为此,你需要将第一行放在<thead>
中,并将内容放在<tbody>
中。
<table class="order">
<thead>
<tr>
<th>物品编号</th>
<th>描述</th>
<th>数量</th>
<th>单价</th>
<th>总计</th>
</tr>
</thead>
<tbody>
(...)
</tbody>
</table>
然后在表格上添加以下CSS
.order {
-fs-table-paginate: paginate;
border-spacing: 0;
}
英文:
You have to use CSS 2.0 paged media to define a header on your HTML document.
- Create a div which contains the content to be repeated as header
<div id="header">
<table>
<tr>
<td>
<img width="80" src="https://simplesolution.dev/images/Logo_S_v1.png" />
</td>
<td>
<h1>Supply Order Form</h1>
</td>
</tr>
</table>
<div>
<table>
<tr>
<td th:text="'Date: ' + ${#dates.format(order.date, 'dd/MM/yyyy')}"></td>
</tr>
(...)
</table>
</div>
</div>
- Add the following CSS to repeat this content on each page
@page {
@top-center { content: element(header); }
margin-top:10cm; /* Adjust this value to the size of the element */
/* you want to repeat on each page */
}
#header {
position: running(header);
}
You will also probably want to repeat the header of the table.
For that, you need to put the first line in a <thead>
and the content in a <tbody>
.
<table class="order">
<thead>
<tr>
<th>Item #</th>
<th>Description</th>
<th>Quantity</th>
<th>Unit Price</th>
<th>Total</th>
</tr>
</thead>
<tbody>
(...)
</tbody>
</table>
And then add the following CSS on the table
.order {
-fs-table-paginate: paginate;
border-spacing: 0;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论