YOUR FEEDBACK
Synchronizing Multiple Exchange Calendars
Jerry Brunning wrote: Sorry, there was a trailing space on the url in the ...
SOA World Conference
Virtualization Conference
$50 Savings Expire May 23, 2008... – Register Today!


2007 West
GOLD SPONSORS:
Active Endpoints
Your SOA Needs BPEL for Orchestration
BEA
Virtualized SOA: Adaptive Infrastructure for Demanding Applications
Nexaweb
Overcoming Bandwidth Challenges with Nexaweb
TIBCO
What is Service Virtualization?
SILVER SPONSORS:
WSO2
Using Web Services Technologies and FOSS Solutions
Click For 2007 East
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
TODAY'S TOP SOA & WEBSERVICES LINKS


Generate PDF Files with XML,XSL-FO, and FOP

Digg This!

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:

  • XSLT: A language for transforming XML documents
  • XSL-FO: An XML vocabulary for specifying formatting semantics

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:
1.   Create an XSL-FO stylesheet.
2.   Transform the XML XSL-FO using JAXP API to produce an intermediate ".fo" file.
3.   Use org.apache.fop API to convert the ".fo" file to PDF.

Creating the XSL-FO Stylesheet

Listing 1 is the outline of a simple XSL-FO stylesheet.

  • <fo:root is the root element.
  • <fo:layout-master-set> encapsulates the simple-page-master.
  • <fo:simple-page-master defines the page height, width, top and bottom margins, and left and right margins. It has a master name that is referred by the page that uses the characteristics of a page defined in the <fo:simple-page-master>.
  • <fo:page-sequence defines the page-sequence-master and refers to the simple-page-master by using the master-reference attribute. This means that the characteristics of a page defined in the simple-page-master will be used by the page-sequence-master using the master-reference attribute. The value specified in the master-reference attribute must be the same in both the simple-page-master and the page-sequence-master. You may also specify the order in which a given simple-page-master will be used by the page-sequence-master.
  • <fo:flow defines the contents of a page that flows into the xsl-region-body.
  • <fo:block defines the block where the actual contents that appear in a PDF document are placed. Each fo:block prints the contents on a new line. The fo:block can contain <fo:inline elements. You may also place tables within an fo:block.

    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.

  • <fo:table: Equivalent to an HTML <TABLE> tag
  • <fo:table-column: Defines the number of columns used in a table - here, one
  • <fo:table-row: Equivalent to an HTML <TR> tag
  • <fo:table-cell: Equivalent to an HTML <TD> tag

    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
    As mentioned earlier, the XML file will be generated dynamically using the values entered in the Subscription.jsp form. Subscription.xsl is applied to the XML document that's created dynamically and transformed using JAXP API.

    Import the following org.apache.fop classes in XSLTOPDFServlet:

    import org.apache.fop.messaging.
    MessageHandler;
    import org.apache.fop.apps.Driver;
    import org.apache.fop.apps.*;
    import org.apache.log.*;

    Using the JAXP API
    The "Subscription.fo" file in Listing 4 created in the previous step (foFile) is used to create the PDF document using the fop API in Listing 5. The complete source code and Subscription.pdf can be downloaded from www.sys-con.com/xml/source.cfm. You can download org.apache.fop API from http://xml.apache.org/fop.

    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.

    About Suresh Selvaraj
    Suresh Selvaraj is a Sun Certified Java Programmer. He currently works as IT Analyst, Java Developer for Tata Consultancy Services in India.

  • XML JOURNAL LATEST STORIES . . .
    3rd International Virtualization Conference & Expo: Themes & Topics
    From Application Virtualization to Xen, a round-up of the virtualization themes & topics being discussed in NYC June 23-24, 2008 by the world-class speaker faculty at the 3rd International Virtualization Conference & Expo being held by SYS-CON Events in The Roosevelt Hotel, in midtown
    JustSystems, SEC, XBRL International, EDGAR Online Lead XBRL Survival Course
    XBRL can help to transform business, dramatically simplifying filing and reporting and improving transparency and information access in ways never before imaginable. Now, SEC Chairman Cox says an XBRL mandate is on its way. In an upcoming Webinar, JustSystems, the SEC, XBRL Internation
    Red Hat Named "Platinum Sponsor" of Virtualization Conference & Expo
    Red Hat is a trusted open source provider. Red Hat offers enterprise customers a long-term plan for building infrastructures on the quality and innovation of open source. Combining open source operating system platform, Red Hat Enterprise Linux, together with applications, management
    Brits Bitch to EC about Microsoft
    Becta, the British Educational and Communications and Technology Agency, has taken its gripes against Microsoft's allegedly 'anti-competitive' licensing policies for schools and the 'interoperability impediments' in Office 2007 to the European Commission, as a contribution to the new a
    EDI to XML: A Practical Approach
    While EDI transactions account for most worldwide commercial activity, XML-based alternatives are beginning to gain traction. According to Forrester Research, stateful XML, stateless XML, and even flat file exchanges are all projected to grow at a faster rate than EDI over the next few
    SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
    SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
    Click to Add our RSS Feeds to the Service of Your Choice:
    Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
    myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
    Publish Your Article! Please send it to editorial(at)sys-con.com!

    Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

    SYS-CON FEATURED WHITEPAPERS


    ADS BY GOOGLE
    BREAKING XML NEWS
    Microsoft Expands List of Formats Supported in Microsoft Office
    Microsoft Corp. is offering customers greater choice and more flexibility among document for