Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate pdf file dynamically from html template and produce table of contents in java

Tags:

java

html

pdf

itext

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).

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.

Can any one suggest how to implement it?

like image 465
SivaTeja Avatar asked May 07 '15 10:05

SivaTeja


1 Answers

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.

If you are looking for open source / free

Yet another Html to Pdf converter ==> http://www.allcolor.org/YaHPConverter/

wkhtmltopdf ==> http://wkhtmltopdf.org/

PDF Box ==> http://pdfbox.apache.org/

itext ( AGPL(free) / commercial licence ) 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.

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.

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.

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.

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,...

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).

Also check links

http://itextpdf.com/sandbox/merge/MergeWithToc

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

like image 110
Raj Avatar answered Oct 13 '22 23:10

Raj