Friday, October 22, 2021

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

PDF format is a popular format for sending receipts, email confirmation, and other documentation and we often have a requirement to create PDF documents using Java, mostly in JSP pages. Since most of the official documentation uses PDF format nowadays, it becomes imperative to support PDF files. Recently I received a couple of questions regarding the 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 a 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 anymore, you need to pay small licensing fees for using it.

Unlike the previous version of iText, iText 4.2, which was released under MPL/LGPL licenses, the latest version of iText requires you to purchase a license as soon as you develop a commercial application and distribute the iText library inside your project or deploy 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 applications or distributing iText with a closed source product. If your project is already using iText then paying this license fee seems to be a better option than migrating your PDF processing code to another Java library.

But, if you are starting a new project, there are a 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 to generate PDF files and support common functionalities, which is what most Java projects needs.



iText

As I said, iText is one of the most popular Java PDF libraries, though it's open-source and free for personal use, you need to buy a license if you are using it commercially. One of the big advantages of using iText is documentation, in fact, there is a book about it (iText in Action), which contains a lot of examples using iText for generating PDF files in Java.

You can consider using iText if you want to serve PDF to the 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 to all your PDF needs in Java. In fact, many popular frameworks and libraries like Jasper reports and Display tag use iText internally. The good thing about iText is that it's available in both Java and C# and you can still use its earlier version, which is less stringent in licensing. Latest version of iText is iText 5.4.2.


iText in Action - Book to learn iText

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, nontrivial, 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 a 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 starts with the introduction of iText and PDF files like what can you do with PDF and how to create a PDF document. It gradually introduced iText's basic and advanced features e.g. adding text at the absolute position, dealing with large tables, manipulating existing PDF documents, copying pages from existing PDF files, etc.

Which open source library to create PDF documents in Java

In short, the 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

The best thing about iText in Action is that it's written by the creator of iText himself, so it's 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 representation), Print, AWT, and PNG, and to a lesser extent, RTF and TXT

You can further utilize 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 the Apache FOP command-line application to transform XML into PDF, PostScript, PCL, and other formats. If you like to use GUI based applications, there is also an AWT-based viewer integrated. 

If you want to create PDF documents from Java or Servlet JSP based Web applications, you can use the library as well. There is good documentation and tutorials on using FOP is available on Apache's site. The 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 the most widely used and feature-rich library. Many Java open source frameworks and library including display tag, Jasper reports, and Spring MVC provide support for iText. 

It has great documentation and a good book iText in Action to quickly learn and use iText in projects. The only thing which can stop someone from using iText is its new AGPL(Affero General Public License) licensing model. 

Apache FOP is the next best open source PDF library for Java developers. In the next couple of tutorials, we will learn how to use iText and Apache FOP to generate PDF documents in both core Java projects as well as on Java web applications built on top of Spring MVC or Struts2 framework.

Other Java and Spring Articles you may like
  • 5 Courses to learn Spring Cloud for Beginners (courses)
  • 10 Advanced Spring Boot Courses for Java developers (courses)
  • Top 7 Courses to learn Microservices in Java (courses)
  • Full 5 Spring Cloud annotations Java programmer should know (cloud)
  • 15 Spring Boot Interview Questions for Java Developers (questions)
  • Top 5 Courses to Learn and Master Spring Cloud (courses)
  • 5 Free Courses to Learn Spring Framework (free classes)
  • 20 Spring Boot Interview Questions for Java developers (questions)
  • 7 Best Spring Courses for Beginners and Experienced (courses)
  • 10 Spring MVC annotations Java developer should learn (annotations)
  • 5 Courses to Learn Spring Security for Beginners (courses)
  • @SpringBootApplication vs. @EnableAutoConfiguration? (answer)
  • 5 Spring Books Experienced Java Developer Should Read (books)
  • Top 5 Frameworks Java Developer Should Know (frameworks)
  • Top 5 Spring Boot Annotations Java Developers should know (read)
  • 10 Free Courses to learn Spring Framework for Beginners (free classes)
  • 10 Free Courses to learn Spring Boot in-depth (free courses)

Thanks for reading this article so far. If you find these best Spring MVC online courses useful, please share them with your friends and colleagues. If you have any questions or feedback, then please drop a note.

P. S. - If you are new to Java development keen to learn the Spring MVC framework and become a full-stack Java developer but are looking for a free online course, you can also check this Angular and Spring MVC course on Udemy. It's complete, and all you need is a free Udemy account to join this course.

6 comments :

swarna 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

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

Anonymous said...

is there a way to convert afp to pdf using any java api?

Post a Comment