Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

XSL Formatting Objects (XSL-FO), Part 2: Work with advanced XSL-FO techniques

Convert HTML documents into formatting objects and then into PDF files

Doug Tidwell (dtidwell@us.ibm.com), developerWorks Cyber Evangelist, IBM
Photo of Doug Tidwell

Doug Tidwell is the developerWorks Cyber Evangelist, helping people use new technologies to solve problems. He has spoken about Web Services and XML to tens of thousands of developers around the world, a number of whom actually stayed awake. He is also the author of O'Reilly's XSLT and a coauthor of O'Reilly's Programming Web Services with SOAP, both of which make excellent gifts for your friends and loved ones.

In a rare brush with greatness, he and his daughter Lily were once trounced by Olympic gold medalist Marion Jones in a game of Whack-a-Mole at the North Carolina State Fair:

  • Dad: How can the game be over already?
  • Lily: No way! Nobody could be that fast! [Dad looks over, sees winning contestant]
  • Dad: Come on, Lily, let's go....

Summary:  This intermediate-level tutorial shows advanced techniques for working with XSL-FO for formatting data, such as handling lists and tables, creating complex documents with multiple layouts, adding repeating elements, and converting HTML elements to formatting objects and thence to PDF documents. Examples include XSLT templates, FO samples, and PDF output files, all processed in Java with the Apache XML Project's Xalan XSLT processor and FOP (Formatting Objects to PDF) tool.

View more content in this series

Date:  11 Feb 2003
Level:  Intermediate PDF:  A4 and Letter (392 KB | 47 pages)Get Adobe® Reader®

Activity:  16876 views
Comments:  

Tutorial introduction and preparation

What this tutorial covers

The XSL Formatting Objects specification, an official recommendation of the W3C that's commonly known as XSL-FO, defines a number of XML tags that describe how something should be rendered. Although XSL-FO contains elements that describe how to render text in nonprint formats such as spoken text, this tutorial introduces how to create portable document format (PDF) files -- the most common use of XSL-FO.

The tutorial takes up where the "XSL Formatting Objects basics" tutorial left off. It covers in detail the formatting of lists, tables, and cross references. The tutorial goes on to demonstrate how to create complex documents with multiple layouts and repeating elements, such as running heads, on the pages. You'll also find out how to create PDF bookmarks and a table of contents. Finally the tutorial shows how to convert HTML elements to formatting objects and then to PDF format. Downloadable code samples make it easy to adapt samples for your own needs.

The code listings in Resources contain a complete XSLT style sheet that converts an XHTML document into a file of formatting objects.

When you've completed this pair of tutorials, you'll be ready to create your own XML applications that use formatting objects to generate high-quality printable documents.


What you need to know already to benefit from this tutorial

This tutorial assumes that you already comprehend the Extensible Markup Language (XML) and know how to work with it and its related technologies, such as XML Stylesheet Language-Transformation (XSLT). It also assumes that you understand the basic structure of XSL-FO documents, how it functions, how to set up page definitions and text specification, add graphics, and control page and column breaks. If you need to brush up your understanding of XSL-FO fundamentals, start with the prequel to this tutorial, " XSL Formatting Objects basics."

The tools used for the examples are written in Java code, but you don't have to understand the Java language to use them.


What you need to know about the software and standards


Figure 1. FOP project logo
FOP project logo

Although you can use other XSL-FO rendering engines, this tutorial is written for the Apache XML Project's FOP (Formatting Objects to PDF) translator. The examples in this tutorial work with FOP Version 0.20.4, which was released on July 5, 2002. If you try them with other versions of FOP, they may or may not work. The XSL-FO spec became an official recommendation of the W3C on 15 October 2001; the FOP tool supports most of the final spec.

We use the FOP tool at developerWorks for two reasons:

  • It's written in the Java language, and so it runs on all the platforms we care about.
  • It's a no-cost, open-source product, and so anyone can afford it.

If you want to immerse yourself in XSL-FO, you can go directly to the source for the spec at the W3C's site (see Resources). Be aware that this is one of the longest documents at the W3C (roughly 400 pages), although most of it is reference information for the many elements and attributes in the XSL-FO tag set. The reference sections -- particularly Appendixes B, C, and D -- are very useful for looking up property names and values. Remember, as of this writing, FOP does not completely support the XSL-FO spec, and so certain property names and values defined by the spec might not be supported by the tool, or they might be supported with slightly different names and values.


What tools you'll need for the tutorial and how to configure them

To go through the exercises in this tutorial, you'll need to have a Java Developer's Kit (JDK) Version 1.3 or later, as well as the FOP package from the Apache XML Project. You can get the FOP package. Download the latest version and unzip it.

Once you have the JDK and FOP installed, you need to set the classpath.

If you want to follow the examples in the tutorial without remembering to adapt them, put the FOP package at c:\fop-0.20.4rc and then set the classpath like this (except all on one line, of course; I've broken the line only to fit within the text column here):

set classpath=.;c:\fop-0.20.4rc\build\fop.jar;c:\fop-0.20.4rc\
lib\avalon-framework-cvs-20020315.jar;c:\fop-0.20.4rc\lib\bati
k.jar;c:\fop-0.20.4rc\lib\xalan-2.3.1.jar;c:\fop-0.20.4rc\lib\
xercesImpl-2.0.1.jar;c:\fop-0.20.4rc\lib\xml-apis.jar;

If you unzip the FOP package somewhere else, you'll need to change the command accordingly. If you're running Linux, use the command export classpath=/usr/bin/fop-0.20.4rc/build/fop.jar:/usr/bin/fop-... and so on.

1 of 12 | Next

Comments



Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML
ArticleID=160113
TutorialTitle=XSL Formatting Objects (XSL-FO), Part 2: Work with advanced XSL-FO techniques
publish-date=02112003
author1-email=dtidwell@us.ibm.com
author1-email-cc=dwxed@us.ibm.com

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.