Comparing Apache FOP with iText

Balaji D Loganathan

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 http://xmlgraphics.apache.org/ site and see how they have integrated Apache Forrest with Apache FOP

iText

  • 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();

PdfWriter.getInstance(document,
			new FileOutputStream("HelloWorld.pdf"));

document.open();
document.add(new Paragraph("Hello World"));
document.close();

To see complete code, click http://itext.ugent.be/library/com/lowagie/examples/general/HelloWorld.java

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 java.io.FileOutputStream(pdffile);
out = new java.io.BufferedOutputStream(out);

  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/.../.../ExampleXM2PDF.java

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.

Comments (8)

  1. Spike - Reply

    March 5, 2008 at 1:54 am

    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.

  2. Kevin Brown - Reply

    April 2, 2008 at 12:31 am

    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
    RenderX

  3. Chetan - Reply

    August 11, 2008 at 10:16 pm

    Hi,

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

    Thanks!!

  4. Rahul - Reply

    November 7, 2008 at 3:22 pm

    Hi,
    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.

    Thanks
    Rahul

  5. Chris - Reply

    February 7, 2009 at 11:13 am

    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.

  6. Satya - Reply

    September 3, 2010 at 7:01 am

    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 "http://msdn.microsoft.com/en-us/library/Aa203691"....

    now am getting as follows ......................

    FOP ExampleXML2PDF

    Preparing...
    Input: XML (C:\exampleWordBasedXSLFO.xml)
    Stylesheet: C:\Office Samples\OfficeWordWordMLtoXSL-FO\Word2FO.xsl
    Output: PDF (C:\out\ResultXML2PDF.pdf)

    Transforming...
    Success!
    Sep 3, 2010 11:24:31 AM org.apache.fop.fo.extensions.svg.SVGElementMapping initialize
    SEVERE: Error while initializing the Batik SVG extensions
    java.lang.NoClassDefFoundError: org.apache.batik.util.XMLResourceDescriptor
    at org.apache.fop.fo.extensions.svg.SVGElementMapping.initialize(SVGElementMapping.java:80)
    at org.apache.fop.fo.ElementMapping.getTable(ElementMapping.java:52)
    at org.apache.fop.fo.ElementMappingRegistry.addElementMapping(ElementMappingRegistry.java:117)
    at org.apache.fop.fo.ElementMappingRegistry.addElementMapping(ElementMappingRegistry.java:96)
    at org.apache.fop.fo.ElementMappingRegistry.setupDefaultMappings(ElementMappingRegistry.java:77)
    at org.apache.fop.fo.ElementMappingRegistry.(ElementMappingRegistry.java:64)
    at org.apache.fop.apps.FopFactory.(FopFactory.java:150)
    at org.apache.fop.apps.FopFactory.newInstance(FopFactory.java:165)
    at com.word.pdf.ExampleXML2PDF.main(ExampleXML2PDF.java:74)
    Sep 3, 2010 11:24:33 AM org.apache.fop.fo.FOTreeBuilder fatalError
    SEVERE: java.lang.NullPointerException
    (Location of error unknown)java.lang.NullPointerException

  7. Jon - Reply

    October 23, 2011 at 2:59 pm

    hi,

    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

    http://www.java4less.com/fopdesigner/fodesigner.php.

    4- Openness: if you go for iText you are using a proprietary API. XSL-FO does not tights you to a vendor or product.

  8. George - Reply

    February 6, 2012 at 7:55 pm

    Does anybody else get a "MimeConstants.MIME_PDF cannot be resolved" error?

    Code: Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

Add a Comment