Wednesday, May 7, 2014

Open source Java Libraries to Create PDF documents - iText vs Apache FOP

PDF format is a popular format for sending receipt, email confirmation and other documentation and we often has requirement to create PDF documents using Java, mostly in JSP pages. Since most of official documentation uses PDF format now days, it becomes imperative to support PDF files. Recently I received couple of question regarding suggestion of open source Java PDF libraries, like which is the best open source PDF library in Java  or should I use iText or Apache FOP in my Java application for PDF processing. These questions motivates me to write this post. In this article, I will share couple of Java based open source PDF libraries, both FREE and with some licensing fees, which you can use to generate PDF documents in Java projects. Many of you would have definitely heard about iText, it's the standard Java library for creating PDF files, but iText 5.0+ is not  free any more, you need to pay small licensing fess for using it. Unlike previous version of iText, iText 4.2, which was released under MPL/LGPL licenses, latest version of iText requires you to purchase a license as soon as you develop a commercial application and distributing the iText library inside your project or deploying it on a network, without disclosing source code of your own applications under AGPL license. This includes serving PDF using JSP pages in Java web application or distributing iText with a closed source product. If your project is already using iText then paying this license fee seems to be better option than migrating your PDF processing code to another Java library, but if you are starting a new project, there are couple of open source Java PDF libraries e.g. Apache FOP (Formatting Objects processor), which you can explore. This is definitely not as feature rich as iText, but can be useful as to generate PDF files and support common functionalities, which is what most of the Java projects needs.


iText

As I said, iText is one of the most popular Java PDF library, though it's open source and free for personal use, you need to buy license if you are using it commercially. One of the big advantage of using iText is documentation, in fact there is a book about it (iText in Action), which contains lot of examples using iText for generating PDF files in Java. You can consider using iText if you want to serve PDF to browser using JSP pages, wants to generate dynamic PDF documents from XML files for sending confirmations, for adding digital signatures to a PDF file, for adding bookmarks, page numbers, watermarks etc in PDF documents, for splitting, concatenating and manipulating PDF pages. In short, it's the solution of all your PDF needs in Java. Infact many popular framework and library e.g. Jasper reports and Display tag uses iText internally. Good thing about iText is that it's available in both Java and C# and you can still use it's earlier version, which is less stringent in licensing. Latest version of iText is iText 5.4.2.

iText in Action - Book to learn iText
Which open source library to create PDF documents in Java
iText in Action, Second Edition is the new version of this book. This new edition covers the latest version of iText with Java 5,  and it lowers the learning curve to its advanced features. Following its simple, non trivial, and practical examples, you'll learn new form types, including AcroForm, explore the XML Forms Architecture (XFA), and discover techniques for linking documents, creating a PDF based on records in a database, and lot more. If your Java project is using iText extensively for report generation, receipt dispatch, generating confirmation documents, it's good to have a copy of iText in Action for reference and quick learning. It start with introduction of iText and PDF files e.g. what can you do with PDF and how to create PDF document. It gradually introduced iText's basic and advanced feature e.g. adding text at absolute position, dealing with large tables, manipulating existing PDF documents, copying pages from existing PDF files etc.

In short, second Edition of this book mainly covers iText 5 along with
  1. How to generate dynamic PDF from XML or a database
  2. Automate static and dynamic XFA forms
  3. How to add digital signatures

Best thing about iText in Action is that its written by the creator of iText himself, so its extensive as well as authoritative.


Apache FOP

Apache FOP stands for Formatting Objects Processor, it's a print formatter driven by XSL based formatting objects (XSL-FO). It is a Java application that reads a formatting object (FO) tree and renders the resulting pages to a specified output. You can use this library to convert your document into PDF format. You can also provide attributes and information while generating PDF documents. Apache FOP output formats currently supported include PDF, PS, PCL, AFP, XML (area tree re presentation), Print, AWT and PNG, and to a lesser extent, RTF and TXT. You can further utilise any other open source library, in case your document format is not supported by Apache FOP, for example you can use Apache POI for dealing with excel files. The primary output target is PDF. What can you do with Apache FOP? Well, FOP provides both an application and a library that converts an XSL FO document into paginated output. You can directly use Apache FOP command line application to transform XML into PDF, PostScript, PCL and other formats. If you like to use GUI based application, there is also an AWT based viewer integrated. If you want to create PDF document from Java or Servlet JSP based Web application, you can use library as well. There is good documentation and tutorials on using FOP is available on Apache's site. Latest version of Apache FOP is FOP 1.1


That's all on this list of Java open source PDF libraries. iText is by default most widely used and feature rich library. Many Java open source framework and library including display tag, Jasper reports and Spring MVC provides support for iText. It has great documentation and a good book iText in Action to quickly learn and use iText in projects. Only thing which can stop someone from using iText is it's new AGPL(Affero General Public License) licensing model. Apache FOP is next best open source PDF library for Java developer. In next couple of tutorials, we will learn how to use iText and Apache FOP to generate PDF document in both core Java projects as well as on Java web applications built on top of Spring MVC or Struts2 framework.

5 comments :

gurla kalpana said...

There are a lot of pdf libraries out there like pdfsharp. I have used almost all of them and itextSharp is unbeatable. It is also one of the oldest and has huge user added codes and hence has matured over the years. Would recommend Itextsharp any day.

Prasad
PDF Tools Online

Ashish P said...

I have used iReport to design the report (with placeholders) and Jasper with Java to fill in dynamic content into the report. Had to go through a learning curve to achieve this but the end result was good. To embed the pdf report in browser I used a servlet.

Anonymous said...

IText has its own free 1 day conference http://itextpdf.com/events/itext-summit-2014 in Germany next month.

DÄ…bek said...

Why you didn't mention about Apache PDFBox?

Somon said...

More then Apache and iText i found Aspose.PDF for Java library better. It provides more features then Apache and iText and although its not free but offers free trial for one month and it is worth purchasing if you want to develop your own application like PDF in java.

Post a Comment