Comparing Apache FOP with iText

03 Mar, 2008

A brief comparision between Apache FOP and iText, especially when to use it for a Java based project. I have also included a small helloworld code snippet of each.

What is Apache Fop?
Apache FOP (Formatting Objects Processor) is a print formatter driven by XSL formatting objects (XSL-FO). Fully written in Java.
Reads xsl:fo and renders to formats like PDF, PS, PCL, AFP, XML (area tree representation), Print, AWT and PNG, and to a lesser extent, RTF and TXT.
What is iText?
iText is a library that allows you to generate PDF files on the fly.
iText enhance web- and other applications with dynamic PDF document generation and/or manipulation.
While the primary target for both of these applications is to generate PDF documents, these two were entirely different Java libraries.
Below is my short comparison on when to use which,
Apache FOP

  • If you want to have a fine grained control over the presentation and the layout of the PDF.
    Why ? FOP is based on MVC pattern and its uses XSL:FO specification
  • If your input data is heavily based on XML.
    Why? Fop main objective is to convert XML to PDF using XSL:FO. While iText have XML2PDF functionality, FOP together with Xalan has much higher control over parsing the XML and rendering the PDF
  • Apart from PDF, RTF & HTML, If you want to support more output formats like PNG, SVG, TXT and so on.
  • If you DONT want to generate PDF that contains 1000+ pages on every instance.
    Why? Apache FOP is known for slow processing power. It wont really fit for the cases where you might need to process and create 1000+ pages in a very short time.
  • If you want to have a externally configurable control over the output format of the PDF.
    Why? You can keep the style sheet (xsl:fo or xslt) out from your classes or package and tell FOP to use this xsl:fo or xslt while rendering PDF
  • If you want to have a PDF view accessibility for all your web pages in a web application.
    Why? Check out the site and see how they have integrated Apache Forrest with Apache FOP


  • If you want to generate PDF on the fly and if you want to add support for annotations, AcroForms, digital signature
  • If you want to generate 100+ PDF which also contains 1000+ pages.
    Why? iText is very reliable from processing perspective and its pretty fast in generating the PDF’s
  • If you want to post-process, manipulate existing PDF documents.
    Why? Because even Apache FOP reckons using iText for this purpose
  • If you want to encrypt your PDF file
    Why? iText libraries were pretty well designed for this
  • While fetching & displaying the database tables on screen (say web or client application), if you want to have instance support for XML, PDF, Excel, CSV outputs.
    Why ? Checkout DisplayTag features and see how it is using iText for PDF

How is it like writing a simple HelloWorld text printed PDF document generation using iText ?

Document document = new Document();
			new FileOutputStream("HelloWorld.pdf"));;
document.add(new Paragraph("Hello World"));

To see complete code, click
How is it like writing a simple HelloWorld text printed PDF document generation using Apache FOP ?

FopFactory fopFactory = FopFactory.newInstance();
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
OutputStream out = new;
out = new;
  Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
  TransformerFactory factory = TransformerFactory.newInstance();
  Transformer transformer = factory.newTransformer(new StreamSource(new File(xsltfileloc)));
  Source src = new StreamSource(new File(xmlfileloc));
  Result res = new SAXResult(fop.getDefaultHandler());
  transformer.transform(src, res);

For brevity, i have not included the input xml and xslt (xsl:fo) code snippets.
To see the complete code, click FopSource/examples/…/…/
Note :
Well, the above points were based on my experience and exposure, if you have something new or see something misinterpreted, then please posts it as your comment.

Working as a Senior Consultant at Xebia India IT Architects Private Limited, Gurgaon, India.
Newest Most Voted
Inline Feedbacks
View all comments
14 years ago

I’ve been using Apache FOP for one of my projects, and I’ve been pretty happy with it. There are also a number of commercial implementations of XSL:FO to PDF translators, which are supposed to be pretty good, but I haven’t needed to go there yet.
I’ve been a little disappointed in trying to find good documentation on XSL:FO – I bought a book, the “XSL Formatting Objects Developer’s Handbook” by Doug Lowell, but I’m kind of annoyed that it approaches FO from the perspective of it being an extension of XSL, rather than demonstrating FO code that can stand on its own.
Supposedly, FO was never intended to stand independently XSL, but I think that was a pretty silly design decision on the part of somebody from a few years back. Generating PDFs from FO using Apache FOP is a perfect example of where it can be better not to bring in the additional headache of XSL if you don’t need to.

Kevin Brown
14 years ago

I’m not sure I get the comparison in the article. The iText code is for generating one document. Need a different document, you need a programmer to change code. Of course, it could be written more generally to read the input from elsewhere (even an XML file) and certainly get the style from elsewhere (like an XSL file).
The FOP code is a generic framework for processing *any* document in XML with XSL. No programmer is required (in the future) to process different documents with different contents.
When extending this concept to different layouts and different formatting and conditional logic …
It is a balance between writing XSL to create FO or writing in Java or C# to create specific documents.
RenderX and our implementation team makes use of iText in many, many situations — mostly all for manipulation of PDFs generated from RenderX.
And we have found the combination of the two is even stronger for applications like dyanmic, custom forms — leveraging XSL FO for complete look and feel while extracting perfect positional information during the formatting process> This information is passed to code based on iText to generate form fields.
As for books on FO, the **best** I have seen is Ken Holman’s “Practical Formatting using XSL-FO”.
Kevin Brown

13 years ago

I’m exploring JasperReports. You guys have any thoughts on that. I found 2 things interesting into that, 1) Designer can design his own XML template using iReports. 2) It internally uses iText APIs for PDF generation.
I don’t know any limitations about JaspeReports. If you guys can talk more..

13 years ago

First of all very interesting blog. i was looking for a thing like this which will give me a nice starting approach towards pdf generation.
Query: i have got a task to convert pcl file to pdf file using java. can you please suggest me any java API which can do this reliably.
i a mean while i am looking for itext, may be it will help me.

13 years ago

I have been using jasperreports for sometime for producing documents such as insurance quotes, cover letters, etc. Jasperreports is NOT very capable for this type of output (I was constrained to use this Jasper by our architects). Apache FOP used to be used but was found to be too slow. I would have preferred to have used iText directly but wasn’t allowed by the architects.

11 years ago

Hi ..i tried with ur given code and I attached all jars which i need to run like…batik1.5.jar,fop.jar,commonlogging1.1.1.jar, but i couldn’t get the appropriate pdf conversion though pdf is generated but it couldn’t open ……
I created xml file as i saved my doc to xml using “″….
now am getting as follows ………………….
Input: XML (C:\exampleWordBasedXSLFO.xml)
Stylesheet: C:\Office Samples\OfficeWordWordMLtoXSL-FO\Word2FO.xsl
Output: PDF (C:\out\ResultXML2PDF.pdf)
Sep 3, 2010 11:24:31 AM initialize
SEVERE: Error while initializing the Batik SVG extensions
java.lang.NoClassDefFoundError: org.apache.batik.util.XMLResourceDescriptor
at org.apache.fop.apps.FopFactory.(
at org.apache.fop.apps.FopFactory.newInstance(
at com.word.pdf.ExampleXML2PDF.main(
Sep 3, 2010 11:24:33 AM fatalError
SEVERE: java.lang.NullPointerException
(Location of error unknown)java.lang.NullPointerException

10 years ago

thanks for the comparison, it is really good. I would like to add some issues to consider in a comparison:
1- license type: Apache license vs GNU. From my point of view Apache licenses are more appropriate for commercial uses
2- there are existing products to add a digital signature to a PDF, so you still can use Apache FOP with digital signature.
3- easy of Learn and develop: what is easier to learn XSL-FO or iText API? I would say XSL-FO is not easy to learn and test, however there are XSL-FO editors for a very reasonable price (less than the price of a book), see for example
4- Openness: if you go for iText you are using a proprietary API. XSL-FO does not tights you to a vendor or product.

10 years ago

Does anybody else get a “MimeConstants.MIME_PDF cannot be resolved” error?
Code: Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

6 years ago

I cannot comment on Apache Fop, as I have not used it, however I have written an entire PDF generation software on the Mainframe using IText. From what I experienced, iText was very fast in generating PDFs, for example 45000 pages were generated in 6 minutes on z/OS . Also iText supports bi-drectional languages such as Arabic pretty well.

1 year ago

I cannot comment on iText but we’ve been using Apache Fop for many years. The combination xsl/xpath xml is very powerfull and fast if you can use it with a streaming xml parser. We used the Saxon prof edition. You need to adjust your xsl files in order to make use of the streaming part.
Back to Apache fop, with some effort you can offload on the fly svg/png rendering while rendering the pdf at the same time. With these things in place we were able to generate 1000+ page reports incuding charts end maps. Didn’t take long.
Downside of streaming/generating on the fly is that you can’t know the amount of pages in advance.
Finally we were satisfied with fop but don’t know if fop gets a lot of support within the future. Will css take over in favor of xsl:fo?

Explore related posts