【问题标题】:Flying Saucer: Convert Multiple Html to 1 PDF document飞碟:将多个 Html 转换为 1 个 PDF 文档
【发布时间】:2015-05-18 17:40:42
【问题描述】:
我有一个包含 4 个 HTML 文件的报告模板。说吧
- p1.html
- p2.html
- p3.html
- p4.html
我使用飞碟分别解析每个页面并使用速度替换占位符并将其成功转换为 pdf。现在的问题是将这 4 个 HTML 页面转换为单个 pdf 文档。
有两种方法可以做到这一点。
合并 HTML
通过合并所有 HTML 文档,然后使用速度填充占位符,这已成功完成,但分页出现问题。我希望将每个 HTML 页面转换为 1 个 pdf 页面,但在这种情况下,所有文本都被合并。
转换后合并 PDF
这种方法对我来说似乎不合适,因为从相应的 HTML 页面分别生成每个 PDF 页面然后合并到 1 个 pdf 文档不是一个可增强的解决方案。
我们将非常感谢您对编码示例的建议。
【问题讨论】:
标签:
java
itext
flying-saucer
【解决方案1】:
我也在寻找一种合并文档的方法。
我确实知道如何在使用单个 HTML 文档时开始新页面。试试这个:
<html>
<head>
<style>
@page { /* default page styles here */
@page p1 { /* page template for first document */ }
@page p2 { /* page template for second document */ }
@page p3 { /* page template for third document */ }
@page p4 { /* page template for fourth document */ }
#p1 { page: p1; } /* tells #p1 to use p1 page template */
#p2 { page: p2; } /* tells #p2 to use p2 page template */
#p3 { page: p3; } /* tells #p3 to use p3 page template */
#p4 { page: p4; } /* tells #p4 to use p4 page template */
</style>
</head>
<body>
</body>
<article id="p1">
<!-- page 1 content here -->
</article>
<article id="p2">
<!-- page 2 content here -->
</article>
<article id="p3">
<!-- page 3 content here -->
</article>
<article id="p4">
<!-- page 4 content here -->
</article>
</html>
我的理解是,当一个元素需要不同的页面模板时,就会添加分页符。
如果您不需要不同的页面模板(页眉、页脚等都相同),那么您可能只要求为每篇文章设置分页符:
article { page-break-before: always; }
【解决方案2】:
我选择合并 HTML 模板并使用以下 CSS 将其分页到需要的地方。
<style type="text/css">
@page { size:letter; padding:0; margin:0.5in 25px 100px 25px;}
*{ font-family: "verdana", tahoma, arial, sans-serif;}
table { -fs-table-paginate: paginate; thead {
display:table-header-group;}}
@page {
@top-center { content: element(header) }
}
@page:first {
margin:30px 25px 100px 25px;
@top-center { content: element() }
}
table.header {
height:100px;
display: block; text-align: center;
position: running(header);
}
div.footer {
display: block; text-align: center;
position: running(footer);
}
div.content {page-break-after: always;}
#footer {
position: running(footer);
text-align: right;
}
#pagenumber:before {
content: counter(page); }
#pagecount:before {
content: counter(pages); }
</style>