User talk:JMehta

How To Generate Read Only PDF Files On The Fly Using FOP
Overview

This How-To shows you how to publish XML documents in HTML and PDF using FOP. It requires no prior knowledge of FOP, XSLT or XSL-FO.

Prerequisites

Here's what you need:

Application Server 	            :  	Websphere 5.1.2

Softwares : Java 1.3.x or later Runtime Environment,XMLSpy recommended XML editor  but you need no fancy XML editor for this How-To. Copying and pasting the sample code snippets into any text editor will do

Libraries needed to implement FOP : xalan-2.4.1.jar, xercesImpl-2.2.1.jar, xml-apis.jar, avalon-framework-4.2.0.jar, batik-all-1.6.jar, fop0.92beta.jar, commons-io-1.1.jar, serializer-2.7.0.jar The FOP distribution includes all libraries that you will need to run a basic FOP installation.

Intended Audience

Beginning FOP users who want to learn how to publish HTML,EXCEL and/or PDF documents from XML data

Introduction

On a recent project, the client had the following requirements:

1. The client valuations statement had to be generated in the form of a PDF document. and this must be downloadable through the web browser.

2.The file needed to be encrypted(read-only) so that the same could be non-editable when viewed using a Adobe Reader.

3.This generated statement must have a provision to be archived so could be accessible at a later date.

We are using Struts framework and Sybase as backend running on UNIX(Readhat Linux 3.0). The XML which is formed using the Java Bit of Code will be converted to corresponding formats (either PDF ,HTML or EXCEL)..

We had little experience with generating PDF documents. We needed to find a pure Java class library that could produce sophisticated PDF documents in a server-side web application. We had two options intitally to go with being iText or FOP. After much of initial R&D we decided to go with  Fop as it seemed very convincing and easy compared to iText.

This article showcases how easily you can generate a PDF’s to be displayed on a web browser using Extensible Stylesheet Language Formatting Objects(XSL-FO) and Formatting Objects for PDF (FOP).Before starting lets see what FOP is all about ,It is an open source API that converts your xml source into PDF, using the formatting tree.Basically there are two parts to the transformation, the first part is the XSLT tranformation and the second involves the FO(Formaatting Objects) that defines the output-independent formatting objects.The homepage of FOP is http:/xml.apache.org/fop".

Generating PDF(A Simple Example)

Guess Its Enough of theory. Lets get our hands dirty by running FOP... So here is a sample XML data file which looks like this: 

We now need to produce a stylesheet that will be used to transform this data file into an FO file. The sample XSL would look like this.                </fo:flow> </fo:page-sequence> </fo:root> </xsl:template> <xsl:template match="data"> <fo:block> <xsl:apply-templates select="name"/> <xsl:apply-templates select="description"/> </fo:block> </xsl:template> <xsl:template match="name"> <fo:block font-size="18pt" font-family="sans-serif" line-height="24pt" space-after.optimum="15pt" background-color="red" color="white" text-align="center" padding-top="3pt"> <xsl:value-of select="."/> </fo:block> </xsl:template> <xsl:template match="description"> <fo:block font-size="12pt" font-family="sans-serif" line-height="15pt" space-after.optimum="3pt" text-align="justify"> <xsl:value-of select="."/> </fo:block> </xsl:template> </xsl:stylesheet>