Create PDF with Java [duplicate]
Asked Answered
S

3

55

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

Spoken answered 8/9, 2011 at 21:56 Comment(1)
Possible duplicate of List of HTML to PDF converters, which is mentioned in Converting HTML Files to PDF.Beefsteak
S
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.

Skepful answered 8/9, 2011 at 22:4 Comment(4)
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.Landseer
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.Skepful
I can also recommend Velocity for outputting data to xml.Businesslike
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.Miyokomizar
N
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.

Normative answered 8/9, 2011 at 22:8 Comment(4)
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.Stomachache
@mvmn: You can buy a commercial license.Josey
Or you can use iText 1.3 which is GNU Affero General Public License v3Sooty
@Stomachache Your link is broken :(Torero
C
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.

Chum answered 8/9, 2011 at 23:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.