Sunday, June 25, 2006

Articles Moved !

Hi ,
Couple of articles from my blog have been moved to my homesite www.ashishware.com.

These include:

There is also an article on Making Animated Collapsible Panel Using Javascript and online demo of these panel.


Bookmark the site www.ashishware.com for new stuff.From now on updates and newer techincal articles will not be posted here. This blog will be maintained for non-techincal stuff.

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.


Site Meter