|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV |
TODAY'S TOP SOA & WEBSERVICES LINKS XML Tips Generate PDF Files with XML,XSL-FO, and FOP
Generate PDF Files with XML,XSL-FO, and FOP
By: Suresh Selvaraj
Sep. 6, 2002 12:00 AM
This article will give you enough information to use the major features of XSL Formatting Objects (XSL-FO) in conjunction with Apache's FOP API for rendering documents in Adobe's Portable Document Format (PDF). The W3C's specification for Extensible Stylesheet Language comes in two parts:
FOP (Formatting Objects Processor), which is part of Apache's XML project, is the world's first print formatter driven by XSL formatting objects. It's a Java application that reads an XSL-FO file and renders the output in PDF format. Other formats supported are XML, SVG, PS, PCL, Print, AWT, MIF, and TXT. To dig deeper, you may want to visit http://xml.apache.org/fop. This tutorial uses Sun's JAXP API for XSLT transformation and Apache's FOP API for rendering PDF output. We'll use a Journal Subscription form that allows the user to enter details like name, payment mode, and bank details to subscribe to a journal. The form is a simple JSP page. Once the form is submitted, the request is forwarded to a servlet that captures the form details and constructs an XML string. XSL-FO stylesheet is applied to the dynamically created XML string and then transformed using JAXP API. The intermediate ".fo" file created as a result of transformation is used as input by the org.apache.fop API for rendering PDF output.
The following steps are used to create our subscription form
in PDF format:
Creating the XSL-FO Stylesheet
Listing 1 is the outline of a simple XSL-FO stylesheet.
To place the contents in each column of a table, insert the fo:block element in the fo:table-cell element as shown in Listing 2. You may nest tables within tables similar to what we do in HTML. To place another table in the preceding example, insert a complete set of <fo:table></fo: table> inside the <fo:block></fo:block>. The Subscription.xsl in our case study uses nested tables, as seen in Listing 3. A stylesheet now needs to be applied to each element in the XML String document that is constructed dynamically in the servlet. For the full source code of Subscription.xsl see the note at the end of this tutorial.
Using the org.apache.fop API
Import the following org.apache.fop classes in XSLTOPDFServlet:
import org.apache.fop.messaging.
Using the JAXP API
I tested this application using BEA WebLogic Server 6.1. For instructions on how to set up and run the example, please refer to the README file included in the zip. If you don't have access to a Web server, download the FOP API and run the standalone Java program that comes as part of the fop API download. For instructions, please refer to the README file included in the zip. XML JOURNAL LATEST STORIES . . .
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING XML NEWS
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||