|
Using FOP with Java - Part 1
by: Samudra Gupta
Introduction
In data-centric applications, you are often required to produce
reports and documents in various formats. One of the challenges
facing the developer world is to find a generic and consistent
way of manipulating a structurally diverse data set to produce
formatted reports. For example, if you write a program to accept
a certain data set and produce a PDF formatted report, than in
order to produce an HTML report on the same data set you might
have to write a different program. In this article, we will see
a relatively new technology involving W3C standard named XSL-FO.
FOP gives us the flexibility to operate on XML structured data,
apply an XSL Stylesheet, do the XSLT transformation and publish
the data in various formats such as PDF, PCL, SVG, TXT and many
other.
What is FOP
Formatting Object Processor (FOP) is a Java based print
formatter based on the XSL-FO (XSL-Formatting Object) standard.
FOP is capable of reading a FO object tree and renders the
resulting page into a specified output format. FOP currently
supports the following output formats:
- PDF (The default output format).
- TXT
- PCL (For HP PCL Printers)
- PS (Postscript)
- SVG (Scalar Vector Graphics)
- XML (Area Tree representation)
- Print
- AWT
- MIF (Maker Interchange Format, used by Adobe)
With the help of FOP, the generated output can be stored in a
file format in the hard drive or can be directly sent to a
different output device such as browser, printer etc.
Getting FOP
You can download the current FOP implementation from
http://xml.apache.org/fop. FOP depends on other .jar files being
in the classpath. These .jar files come bundled with the FOP
distribution. You can refer to the documents in the FOP
distribution for the .jar files that need to be in the
classpath.
Basics of FOP
One of the major benefits of FOP is it is so simple to use.
Again, it is simple because it works on a very simple
foundation. In this section, we will examine the basic working
model of FOP.
Black-box view
The black-box view of FOP is like this:
In the black-box view, the user provides a XML input and a XSL
to the FOP engine. The FOP engine outputs either a XSL-FO file
(.fo extension) or renders the documents in PDF, SVG, PS etc.
For example, let us consider that you have a simple XML file
data.xml (Listing-1). You want to produce a PDF file out of that
using the XML pdfGen.xml (Listing-2). You can use FOP command
line in the following manner:
fop -xsl pdfGen.xsl -xml data.xml -pdf data.pdf
This will generate a data.pdf file containing the data specified
in the data.xml file and formatted according the formatting
routines specified in the pdfGen.xsl file.
Figure -1 shows the PDF output.
Figure-1: The sample PDF output
White-box view
The white-box view of FOP presents different internal components
of FOP. In general, the FOP consists of the following different
component sections.
- Parsing: Parsing of the XML input and the XSL-FO input. FOP uses SAX to parse the input XML and XSL-FO file and prepares the information for the core FOP engine. At this stage, files are checked for well formed. If the files are well-formed, then XSLT is used to produce an intermediate FO object tree.
- Layout: The Layout managers understand a FO-tree and convert them to the target output layout system. The Layout managers sit between the raw FO documents and the final output.
- Rendering: Renderer objects are responsible for producing the final output in a predefined output format.
With this we finish the introduction to FOP and next we will
see some practical example of using FOP.
Using FOP with Java - Part 2
Graphics with FOP - Part 3
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|