java 从html模板动态生成pdf文件并在java中生成目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30098293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate pdf file dynamically from html template and produce table of contents in java
提问by SivaTeja
We have an editor which is used to design templates for catalogs. it is basically for designing purpose. First, we design a template in the editor and bind our data in that template format. We then get the template in HTML format from front end. From that template, I need to bind the data and generate pdf file with table of contents (index).
我们有一个编辑器,用于设计目录模板。它基本上是为了设计目的。首先,我们在编辑器中设计一个模板并以该模板格式绑定我们的数据。然后我们从前端获取 HTML 格式的模板。从该模板中,我需要绑定数据并生成带有目录(索引)的 pdf 文件。
Many suggested to use itext
library. But my requirement is not to convert html page to pdf. I have to dump all my products data according to the html template which is dynamically generated by user.
许多人建议使用itext
图书馆。但我的要求不是将 html 页面转换为 pdf。我必须根据用户动态生成的 html 模板转储我所有的产品数据。
Can any one suggest how to implement it?
任何人都可以建议如何实施它吗?
回答by Raj
There are some open source and some paid options. itext imho would bring out the best result if you can use a product under AGPL license.
有一些开源和一些付费选项。如果您可以使用 AGPL 许可下的产品,那么 itext imho 会带来最好的结果。
If you are looking for open source / free
如果您正在寻找开源/免费
Yet another Html to Pdf converter ==> http://www.allcolor.org/YaHPConverter/
另一个 Html 到 Pdf 转换器 ==> http://www.allcolor.org/YaHPConverter/
wkhtmltopdf ==> http://wkhtmltopdf.org/
wkhtmltopdf ==> http://wkhtmltopdf.org/
PDF Box ==> http://pdfbox.apache.org/
PDF 盒 ==> http://pdfbox.apache.org/
itext ( AGPL(free) / commercial licence ) http://itextpdf.com/pricing
itext(AGPL(免费)/商业许可)http://itextpdf.com/pricing
If you are looking for paid(and almost always easier)
如果您正在寻找付费(而且几乎总是更容易)
http://pdfcrowd.com/web-html-to-pdf-java/
http://pdfcrowd.com/web-html-to-pdf-java/
http://www.aspose.com/docs/display/pdfjava/Convert+HTML+to+PDF+Format
http://www.aspose.com/docs/display/pdfjava/Convert+HTML+to+PDF+Format
Regarding TOC
关于TOC
This answer is provided by Bruno Lowagie creater of itext.
此答案由itext 的 Bruno Lowagie 创建者提供。
http://support.itextpdf.com/node/113
http://support.itextpdf.com/node/113
There are different ways to solve this problem.
有不同的方法可以解决这个问题。
Solution 1: you could create a PDF with bookmarks in a first pass, without worrying about a table of contents. Then in a second pass, you take the PDF you've generated to create a TOC based on the bookmarks and create a new PDF with the TOC, followed by the content.
解决方案 1:您可以在第一遍创建带有书签的 PDF,而无需担心目录。然后在第二遍中,您使用生成的 PDF 创建基于书签的 TOC,并使用 TOC 创建一个新的 PDF,然后是内容。
Solution 2: you could create two PDF files simultaneously, one with the actual content, one with the TOC. Once you've created all the content, you can concatenate both files: first the TOC, then the actual content.
解决方案 2:您可以同时创建两个 PDF 文件,一个包含实际内容,一个包含 TOC。创建所有内容后,您可以连接两个文件:首先是目录,然后是实际内容。
Solution 3: you can keep all the entries for the TOC in memory, add these entries at the end of the document, and then reorder the pages.
解决方案 3:您可以将 TOC 的所有条目保留在内存中,将这些条目添加到文档末尾,然后重新排列页面顺序。
These three solutions are the first that come to mind (based on examples in the book); there may be other ways to do it.
这三个解决方案是第一个想到的(基于书中的例子);可能还有其他方法可以做到。
There's one major difficulty you need to take into account: what about page numbers?
您需要考虑一个主要困难:页码呢?
If you look at a Manning book (for example "iText in Action"), you'll see that the TOC is numbered i, ii, iii, iv, v,... whereas the actual content is numbered 1, 2, 3, 4, 5,...
如果您查看 Manning 的书(例如“iText in Action”),您会看到 TOC 编号为 i、ii、iii、iv、v...,而实际内容编号为 1、2、3 , 4, 5, ...
If you want to avoid this, if you want to number the pages 1, 2, 3, 4, 5, 6, 7,... from the start, a solution where you create the PDF "in two passes" is best, because you can wait to add the page numbers until the second pass (only then you know how many pages there are in the TOC).
如果您想避免这种情况,如果您想从一开始就对第 1、2、3、4、5、6、7 页进行编号,那么“分两次”创建 PDF 的解决方案是最好的,因为您可以等到第二遍后再添加页码(只有这样您才能知道 TOC 中有多少页)。
Also check links
还要检查链接
http://itextpdf.com/sandbox/merge/MergeWithToc
http://itextpdf.com/sandbox/merge/MergeWithToc