58

Possible Duplicate:
PDF Generation Library for Java

I'm working on an invoice program for a local accounting company. What is a good way to create a PDF file with Java? Any good library? I'm totally new to PDF export (On any language).

lakshman
  • 2,641
  • 6
  • 37
  • 63
hogni89
  • 1,920
  • 6
  • 22
  • 39
  • 1
    Possible duplicate of [List of HTML to PDF converters](http://stackoverflow.com/questions/3178448/list-of-html-to-pdf-converters), which is mentioned in [Converting HTML Files to PDF](http://stackoverflow.com/questions/633780/converting-html-files-to-pdf). – trashgod Sep 23 '11 at 15:58

3 Answers3

86

I prefer outputting my data into XML (using Castor, XStream or JAXB), then transforming it using a XSLT stylesheet into XSL-FO and render that with Apache FOP into PDF. Worked so far for 10-page reports and 400-page manuals. I found this more flexible and stylable than generating PDFs in code using iText.

Scott
  • 3
  • 3
Philipp Reichart
  • 20,771
  • 6
  • 58
  • 65
  • How fast can this convert a 400 page manual into PDF? I've used iText, but it's too slow for some of our large documents. – James Drinkard Mar 04 '13 at 16:37
  • Sorry, I did this back in 2005-2007, but IIRC it was sub-second for a simple 10-page report (with a cached stylesheet) and dozens of seconds for a more complex 400 page report. Do cache the parsed XSL stylesheet when you need to generate same/similar reports with different data -- makes for quite a boost. – Philipp Reichart Mar 04 '13 at 18:20
  • 1
    I can also recommend Velocity for outputting data to xml. – Koray Tugay Nov 24 '14 at 18:37
  • 2
    You could also use freemarker to output xml/html and pump the string into iText. It's a lot like how you do it, but freemarker is a very good template engine. – Patrick W. McMahon Jun 03 '15 at 14:40
37

Following are few libraries to create PDF with Java:

  1. iText
  2. Apache PDFBox
  3. BFO

I have used iText for genarating PDF's with a little bit of pain in the past.

Or you can try using FOP: FOP is an XSL formatter written in Java. It is used in conjunction with an XSLT transformation engine to format XML documents into PDF.

Lii
  • 11,553
  • 8
  • 64
  • 88
Ritesh Mengji
  • 6,000
  • 9
  • 30
  • 46
  • 36
    On iText site I read that their AGPL licence requires from you "distribution of all source code including your own product, including if it is a web-based application", among other things. Seems to be completely unacceptable for any commercial project. – mvmn Jan 24 '13 at 13:13
  • @mvmn: You can buy a commercial license. – Thilo Mar 25 '14 at 05:43
  • Or you can use iText 1.3 which is GNU Affero General Public License v3 – Sully May 21 '14 at 14:25
  • 3
    @mvmn Your link is broken :( – user3738870 Apr 20 '20 at 18:07
17

Another alternative would be JasperReports: JasperReports Library. It uses iText itself and is more than a PDF library you asked for, but if it fits your needs I'd go for it.

Simply put, it allows you to design reports that can be filled during runtime. If you use a custom datasource, you might be able to integrate JasperReports easily into the existing system. It would save you the whole layouting troubles, e.g. when invoices span over more sites where each side should have a footer and so on.

UdayKiran Pulipati
  • 6,579
  • 7
  • 67
  • 92
box
  • 3,156
  • 3
  • 26
  • 36