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="http://www.w3.org/1999/XSL/Format">

<!-- defines the layout master -->
<fo:layout-master-set>
<fo:simple-page-master master-name="first"
page-height="29.7cm"
page-width="21cm"
margin-top="1cm"
margin-bottom="2cm"
margin-left="2.5cm"
margin-right="2.5cm">
<fo:region-body margin-top="1cm" margin-bottom="1.5cm"/>
<fo:region-before extent="3cm"/>
<fo:region-after extent="1.5cm"/>
</fo:simple-page-master>
</fo:layout-master-set>

<!-- 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
</fo:block>

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

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

<!-- 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-body>
<fo:table-row>
<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-row>
<fo:table-row>
<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-row>
<fo:table-row>
<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-row>
<fo:table-row>
<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>
</fo:table-row>
</fo:table-body>
</fo:table>
<!-- table end -->
</fo:flow>
</fo:page-sequence>
</fo:root>
Save this in a file called test.fo. Now run fop.bat (or fop.sh on Unix/Linux)
: >fop.bat test.fo 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.

5 comments:

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!

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

Thanks.

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

Hi,

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.


Site Meter