Saturday, June 03, 2006

Creating PDFs with Apache FOP

FOP (Formatting Objects Processor) is from the apache group.It is said to be the worlds first print formatter driven by XSL formatting objects (XSL-FO) and the world's first output independent formatter. Actually it isn’t a tool for specifically meant for PDF conversion or creation.FOP can be downloaded here.

But, its ability to convert XSL files (*.fo) to PDF certainly makes it worth mention . Basically FOP is great for template based PDF generation. Simple programs can fetch data from any data source and populate such a template , and generate PDF’s on fly !

Its not that it is not very flexible, but generation XSL files for complex PDF’s can be a big pain. It is a Java application that reads a formatting object (FO) tree and renders the resulting pages to a specified output.It comes with nice documentation and samples that get you started within minutes.

Before you start using FOP, you should have look at samples in \examples\fo\ folders particularly \examples\fo\basic.I think you will learn things more quickly than reading the reference document (xslfoRef.pdf). Given below is a very simple *.fo file that includes a heading,some text,hyperlink , table and a image.

<?xml version="1.0" ?>
<fo:root xmlns:fo="">

<!-- defines the layout master -->
<fo:simple-page-master master-name="first"
<fo:region-body margin-top="1cm" margin-bottom="1.5cm"/>
<fo:region-before extent="3cm"/>
<fo:region-after extent="1.5cm"/>

<!-- starts actual layout -->
<fo:page-sequence master-reference="first">
<fo:flow flow-name="xsl-region-body">

<!-- Heading -->
<fo:block font-family="Helvetica" background-color="blue"
color="white" text-align="center" font-size="14pt">
FOP Demonstration

<!-- Image-->
<fo:block space-before.optimum="20pt">
<fo:external-graphic src="url(test.jpg)"/>

<!-- Hyperlink -->
<fo:block space-before.optimum="10pt" text-align="left" >
You can download FOP here:
<fo:basic-link external-destination="" text-decoration="underline" color="blue">

<!-- table start -->
<fo:block space-before.optimum="10pt" >A Table</fo:block>
<fo:table table-layout="fixed" border-width="0.1mm" border-style="solid" background-color="yellow">
<fo:table-column column-width="50mm"/>
<fo:table-column column-width="50mm"/>
<fo:table-column column-width="50mm"/>
<fo:table-cell border-style="solid" ><fo:block>good</fo:block></fo:table-cell>
<fo:table-cell border-style="solid"><fo:block>bad</fo:block></fo:table-cell>
<fo:table-cell border-style="solid"><fo:block>ugly</fo:block></fo:table-cell>
<fo:table-cell border-style="solid"><fo:block>nice</fo:block></fo:table-cell>
<fo:table-cell border-style="solid"><fo:block>dice</fo:block></fo:table-cell>
<fo:table-cell border-style="solid"><fo:block>vice</fo:block></fo:table-cell>
<fo:table-cell border-style="solid"><fo:block>literature</fo:block></fo:table-cell>
<fo:table-cell border-style="solid"><fo:block>music</fo:block></fo:table-cell>
<fo:table-cell border-style="solid"><fo:block>art</fo:block></fo:table-cell>
<fo:table-cell border-style="solid"><fo:block>java</fo:block></fo:table-cell>
<fo:table-cell border-style="solid"><fo:block>perl</fo:block></fo:table-cell>
<fo:table-cell border-style="solid"><fo:block>python</fo:block></fo:table-cell>
<!-- table end -->
Save this in a file called Now run fop.bat (or on Unix/Linux)
: >fop.bat test.pdf

The test PDF in this example will look like this :

FOP is ideal for generation of simple PDF's from templates. It is a powerful alternative for server side PDF creation in all platforms supporting Java.


Anonymous said...

Hi, I was out blogging and found your site. It certainly got my attention and interest. I was looking for Harnesses information and even though this isn't a perfect match I enjoyed your site. Thanks for the read!

rampi said...

I am sorry, I am pasting a comment on the wrong page. I had earlier seen a good write up on transliteration systems but now link to that seems to be invalid. I have been working on a predictive transliteration system which is fuzzy as well as it does context(character level) based transliteration . I would like you to have a look at my blog and also the online demos available from there and leave your comments.


Anonymous said...

It's very helpful for me.
But I have one question how to put a image for FOP.
I see the example FOP code fo:external-graphic src="url(test.jpg)"

This line mean to read test.jpg in the same path that we put this XML.
Is it correct ?

Anonymous said...


I was searching for tutorial on apache fop. And finally i landed your blog. Truely its informative.
Keep up your good work.

- Meena

Sridhar Vasudevan said...

this is very very good blog for beginners like me. it gave me a great idea. thank u very much.