Saturday 29 November 2014

HTML to PDF in Java - Part 1

Reporting in web can assume multiple formats but the most popular is PDF.PDF report is more often designed as a snapshot of the web page.There are number of Commercial and open source libraries in java for PDF generation.In my coding experience,I came across 2 patterns.

Code Entire Report in Java

When?

If the report is a small i.e a page which contains very limited components.

Pros:
1.Reports can be extracted to a template but format supported is mostly PDF

Cons:
1.Since Code is in Java,changes can't be done on the fly.For every change in PDF code needs to be changed. 
2.Achieving the layout of the components is a herculean task.
3.Reports with huge dataset might take long time and result in timeout.
4.Memory utilization will also be high for large data set.


HTML to PDF in Java

When?

If the report is complex i.e SVG images, charts, tables with huge record set.

Pros:
1.Any Java template engine can be used as all we need is HTML/XHTML.
2.Layout of the report can be changed on the fly.
3.components styles are from the included CSS files. So components in PDF can be customized on the fly.
4.Generation of Reports for Huge data sets is possible.


Cons:
1.HTML/XHTML needs to be well formed and adhere to w3c standards.
2.SVG images are not supported by  the popular PDF rendering libraries.We have to use Apache PDFTranscoder to render the SVG.
3.It is slow for big HTML pages as every element has to be parsed and rendered.

No comments:

Post a Comment