从html模板动态生成pdf文件,并在java中生成目录

时间:2022-03-12 00:26:08

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?

任何人都可以建议如何实施它?

1 个解决方案

#1


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 Box ==> 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://docraptor.com/

http://pd4ml.com/

http://pdfcrowd.com/web-html-to-pdf-java/

http://www.aspose.com/docs/display/pdfjava/Convert+HTML+to+PDF+Format

http://www.princexml.com/

Regarding TOC

This answer is provided by Bruno Lowagie creater of itext.

这个答案由itext的Bruno Lowagie creater提供。

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。一旦创建了所有内容,就可以连接两个文件:首先是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

How to generate a Table of Contents "TOC" with iText?

如何使用iText生成目录“TOC”?

#1


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 Box ==> 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://docraptor.com/

http://pd4ml.com/

http://pdfcrowd.com/web-html-to-pdf-java/

http://www.aspose.com/docs/display/pdfjava/Convert+HTML+to+PDF+Format

http://www.princexml.com/

Regarding TOC

This answer is provided by Bruno Lowagie creater of itext.

这个答案由itext的Bruno Lowagie creater提供。

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。一旦创建了所有内容,就可以连接两个文件:首先是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

How to generate a Table of Contents "TOC" with iText?

如何使用iText生成目录“TOC”?