The Apache FOP Project

The Apache™ FOP Project

Apache™ FOP: Embedding

How to Embed FOP in a Java application

Overview

Review Running Apache™ FOP for important information that applies to embedded applications as well as command-line use, such as options and performance.

To embed Apache™ FOP in your application, first create a new org.apache.fop.apps.FopFactory instance. This object can be used to launch multiple rendering runs. For each run, create a new org.apache.fop.apps.Fop instance through one of the factory methods of FopFactory. In the method call you specify which output format (i.e. MIME type) to use and, if the selected output format requires an OutputStream, which OutputStream to use for the results of the rendering. You can customize FOP's behaviour in a rendering run by supplying your own FOUserAgent instance. The FOUserAgent can, for example, be used to set your own document handler instance (details below). Finally, you retrieve a SAX DefaultHandler instance from the Fop object and use that as the SAXResult of your transformation.

The API

FOP has many classes which express the "public" access modifier, however, this is not indicative of their inclusion into the public API. Every attempt will be made to keep the public API static, to minimize regressions for existing users, however, since the API is not clearly defined, the list of classes below are the generally agreed public API:

Basic Usage Pattern

Apache FOP relies heavily on JAXP. It uses SAX events exclusively to receive the XSL-FO input document. It is therefore a good idea that you know a few things about JAXP (which is a good skill anyway). Let's look at the basic usage pattern for FOP...

Here is the basic pattern to render an XSL-FO file to PDF:

import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.MimeConstants;

/*..*/

// Step 1: Construct a FopFactory by specifying a reference to the configuration file
// (reuse if you plan to render multiple documents!)
FopFactory fopFactory = FopFactory.newInstance(new File("C:/Temp/fop.xconf"));

// Step 2: Set up output stream.
// Note: Using BufferedOutputStream for performance reasons (helpful with FileOutputStreams).
OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("C:/Temp/myfile.pdf")));

try {
    // Step 3: Construct fop with desired output format
    Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

    // Step 4: Setup JAXP using identity transformer
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(); // identity transformer

    // Step 5: Setup input and output for XSLT transformation
    // Setup input stream
    Source src = new StreamSource(new File("C:/Temp/myfile.fo"));

    // Resulting SAX events (the generated FO) must be piped through to FOP
    Result res = new SAXResult(fop.getDefaultHandler());

    // Step 6: Start XSLT transformation and FOP processing
    transformer.transform(src, res);

} finally {
    //Clean-up
    out.close();
}

Let's discuss these 5 steps in detail:

If you're not totally familiar with JAXP Transformers, please have a look at the Embedding examples below. The section contains examples for all sorts of use cases. If you look at all of them in turn you should be able to see the patterns in use and the flexibility this approach offers without adding too much complexity.

This may look complicated at first, but it's really just the combination of an XSL transformation and a FOP run. It's also easy to comment out the FOP part for debugging purposes, for example when you're tracking down a bug in your stylesheet. You can easily write the XSL-FO output from the XSL transformation to a file to check if that part generates the expected output. An example for that can be found in the Embedding examples (See "ExampleXML2FO").

Logging

Logging is now a little different than it was in FOP 0.20.5. We've switched from Avalon Logging to Jakarta Commons Logging. While with Avalon Logging the loggers were directly given to FOP, FOP now retrieves its logger(s) through a statically available LogFactory. This is similar to the general pattern that you use when you work with Apache Log4J directly, for example. We call this "static logging" (Commons Logging, Log4J) as opposed to "instance logging" (Avalon Logging). This has a consequence: You can't give FOP a logger for each processing run anymore. The log output of multiple, simultaneously running FOP instances is sent to the same logger.

By default, Jakarta Commons Logging uses JDK logging (available in JDKs 1.4 or higher) as its backend. You can configure Commons Logging to use an alternative backend, for example Log4J. Please consult the documentation for Jakarta Commons Logging on how to configure alternative backends.

As a result of the above we differentiate between two kinds of "logging":

The use of "feedback" instead of "logging" is intentional. Most people were using log output as a means to get feedback from events within FOP. Therefore, FOP now includes an event package which can be used to receive feedback from the layout engine and other components within FOP per rendering run. This feedback is not just some text but event objects with parameters so these events can be interpreted by code. Of course, there is a facility to turn these events into normal human-readable messages. For details, please read on on the Events page. This leaves normal logging to be mostly a thing used by the FOP developers although anyone can surely activate certain logging categories but the feedback from the loggers won't be separated by processing runs. If this is required, the Events subsystem is the right approach.

Processing XSL-FO

Once the Fop instance is set up, call getDefaultHandler() to obtain a SAX DefaultHandler instance to which you can send the SAX events making up the XSL-FO document you'd like to render. FOP processing starts as soon as the DefaultHandler's startDocument() method is called. Processing stops again when the DefaultHandler's endDocument() method is called. Please refer to the basic usage pattern shown above to render a simple XSL-FO document.

Processing XSL-FO generated from XML+XSLT

If you want to process XSL-FO generated from XML using XSLT we recommend again using standard JAXP to do the XSLT part and piping the generated SAX events directly through to FOP. The only thing you'd change to do that on the basic usage pattern above is to set up the Transformer differently:

//without XSLT:
//Transformer transformer = factory.newTransformer(); // identity transformer

//with XSLT:
Source xslt = new StreamSource(new File("mystylesheet.xsl"));
Transformer transformer = factory.newTransformer(xslt);

Input Sources

The input XSL-FO document is always received by FOP as a SAX stream (see the Parsing Design Document for the rationale).

However, you may not always have your input document available as a SAX stream. But with JAXP it's easy to convert different input sources to a SAX stream so you can pipe it into FOP. That sounds more difficult than it is. You simply have to set up the right Source instance as input for the JAXP transformation. A few examples:

There are a variety of upstream data manipulations possible. For example, you may have a DOM and an XSL stylesheet; or you may want to set variables in the stylesheet. Interface documentation and some cookbook solutions to these situations are provided in Xalan Basic Usage Patterns.

Configuring Apache FOP Programmatically

Apache FOP provides two levels on which you can customize FOP's behaviour: the FopFactory and the user agent.

Customizing the FopFactory

The FopFactory holds configuration data and references to objects which are reusable over multiple rendering runs. It's important to instantiate it only once (except in special environments) and reuse it every time to create new FOUserAgent and Fop instances.

The FopFactoryBuilder is used to construct a FopFactory object. This builder can be used to set configuration values which will determine the behaviour of the FopFactory object. To create the FopFactoryBuilder the following line can be used as a precursor for the following examples:

FopFactoryBuilder builder = new FopFactoryBuilder(baseURI);

Set a URIResolver for custom URI resolution. By supplying a JAXP URIResolver you can add custom URI resolution functionality to FOP. For example:

// myResourceResolver is a org.apache.xmlgraphics.io.ResourceResolver
FopFactoryBuilder builder = new FopFactoryBuilder(baseURI, myResourceResolver);

The following example shows how a FopFactory is created using the settings specified:

FopFactory fopFactory = builder.build();

Finally, there are several options which can be set on the FopFactory itself including the following example:

Customizing the User Agent

The user agent is the entity that allows you to interact with a single rendering run, i.e. the processing of a single document. If you wish to customize the user agent's behaviour, the first step is to create your own instance of FOUserAgent using the appropriate factory method on FopFactory and pass that to the factory method that will create a new Fop instance:

FopFactory fopFactory = FopFactory.newInstance(); // Reuse the FopFactory if possible!
// do the following for each new rendering run
FOUserAgent userAgent = fopFactory.newFOUserAgent();
// customize userAgent
Fop fop = fopFactory.newFop(MimeConstants.MIME_POSTSCRIPT, userAgent, out);

You can do all sorts of things on the user agent:

You should not reuse an FOUserAgent instance between FOP rendering runs although you can. Especially in multi-threaded environment, this is a bad idea.

Using a Configuration File

Instead of setting the parameters manually in code as shown above you can also set many values from an XML configuration file:

import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;

/*..*/

DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
Configuration cfg = cfgBuilder.buildFromFile(new File("C:/Temp/mycfg.xml"));
fopFactoryBuilder = new FopFactoryBuilder(baseURI).setConfiguration(cfg);

The layout of the configuration file is described on the Configuration page.

Document Handlers

The document handlers are classes that inherit from org.apache.fop.render.intermediate.IFDocumentHandler. This is an interface for which a MIME type specific implementation can be created. This same handler is used either when XSL-FO is used as the input or when Intermediate Format is used. Since IF is output format agnostic, if custom fonts or other configuration information that affect layout (specific to a particular MIME type) are given then FOP needs that contextual information. The document handler provides that context so that when the IF is rendered, it is more visually consistent with FO rendering. The code below shows an example of how a document handler can be used to provide PDF configuration data to the IFSerializer.

IFDocumentHandler targetHandler = userAgent.getRendererFactory().createDocumentHandler(userAgent, MimeConstants.MIME_PDF);

IFSerializer ifSerializer = new IFSerializer(new IFContext(userAgent));  //Create the IFSerializer to write the intermediate format
ifSerializer.mimicDocumentHandler(targetHandler);   //Tell the IFSerializer to mimic the target format

userAgent.setDocumentHandlerOverride(ifSerializer);  //Make sure the prepared document handler is used

The rest of the code is the same as in Basic Usage Patterns.

Hints

Object reuse

Fop instances shouldn't (and can't) be reused. Please recreate Fop and FOUserAgent instances for each rendering run using the FopFactory. This is a cheap operation as all reusable information is held in the FopFactory. That's why it's so important to reuse the FopFactory instance.

AWT issues

If your XSL-FO files contain SVG then Apache Batik will be used. When Batik is initialised it uses certain classes in java.awt that intialise the Java AWT classes. This means that a daemon thread is created by the JVM and on Unix it will need to connect to a DISPLAY.

The thread means that the Java application may not automatically quit when finished, you will need to call System.exit(). These issues should be fixed in the JDK 1.4.

If you run into trouble running FOP on a head-less server, please see the notes on Batik.

Getting information on the rendering process

To get the number of pages that were rendered by FOP you can call Fop.getResults(). This returns a FormattingResults object where you can look up the number of pages produced. It also gives you the page-sequences that were produced along with their id attribute and their numbers of pages. This is particularly useful if you render multiple documents (each enclosed by a page-sequence) and have to know the number of pages of each document.

Improving performance

There are several options to consider:

Multithreading FOP

Apache FOP may currently not be completely thread safe. The code has not been fully tested for multi-threading issues, yet. If you encounter any suspicious behaviour, please notify us.

There is also a known issue with fonts being jumbled between threads when using the Java2D/AWT renderer (which is used by the -awt and -print output options). In general, you cannot safely run multiple threads through the AWT renderer.

Examples

The directory "{fop-dir}/examples/embedding" contains several working examples.

ExampleFO2PDF.java

This example demonstrates the basic usage pattern to transform an XSL-FO file to PDF using FOP.

Example XSL-FO to PDF

ExampleXML2FO.java

This example has nothing to do with FOP. It is there to show you how an XML file can be converted to XSL-FO using XSLT. The JAXP API is used to do the transformation. Make sure you've got a JAXP-compliant XSLT processor in your classpath (ex. Xalan).

Example XML to XSL-FO

ExampleXML2PDF.java

This example demonstrates how you can convert an arbitrary XML file to PDF using XSLT and XSL-FO/FOP. It is a combination of the first two examples above. The example uses JAXP to transform the XML file to XSL-FO and FOP to transform the XSL-FO to PDF.

Example XML to PDF (via XSL-FO)

The output (XSL-FO) from the XSL transformation is piped through to FOP using SAX events. This is the most efficient way to do this because the intermediate result doesn't have to be saved somewhere. Often, novice users save the intermediate result in a file, a byte array or a DOM tree. We strongly discourage you to do this if it isn't absolutely necessary. The performance is significantly higher with SAX.

ExampleObj2XML.java

This example is a preparatory example for the next one. It's an example that shows how an arbitrary Java object can be converted to XML. It's an often needed task to do this. Often people create a DOM tree from a Java object and use that. This is pretty straightforward. The example here, however, shows how to do this using SAX, which will probably be faster and not even more complicated once you know how this works.

Example Java object to XML

For this example we've created two classes: ProjectTeam and ProjectMember (found in xml-fop/examples/embedding/java/embedding/model). They represent the same data structure found in xml-fop/examples/embedding/xml/xml/projectteam.xml. We want to serialize to XML a project team with several members which exist as Java objects. Therefore we created the two classes: ProjectTeamInputSource and ProjectTeamXMLReader (in the same place as ProjectTeam above).

The XMLReader implementation (regard it as a special kind of XML parser) is responsible for creating SAX events from the Java object. The InputSource class is only used to hold the ProjectTeam object to be used.

Have a look at the source of ExampleObj2XML.java to find out how this is used. For more detailed information see other resources on JAXP (ex. An older JAXP tutorial).

ExampleObj2PDF.java

This example combines the previous and the third to demonstrate how you can transform a Java object to a PDF directly in one smooth run by generating SAX events from the Java object that get fed to an XSL transformation. The result of the transformation is then converted to PDF using FOP as before.

Example Java object to PDF (via XML and XSL-FO)

ExampleDOM2PDF.java

This example has FOP use a DOMSource instead of a StreamSource in order to use a DOM tree as input for an XSL transformation.

ExampleSVG2PDF.java (PDF Transcoder example)

This example shows the usage of the PDF Transcoder, a sub-application within FOP. It is used to generate a PDF document from an SVG file.

ExampleConcat.java (IF Concatenation example)

This can be found in the embedding.intermediate package within the examples and describes how IF can be concatenated to produce a document. Because IF has been through FOPs layout engine, it should be visually consistent with FO rendered documents while allowing the user to merge numerous documents together.

Final notes

These examples should give you an idea of what's possible. It should be easy to adjust these examples to your needs. Also, if you have other examples that you think should be added here, please let us know via either the fop-users or fop-dev mailing lists. Finally, for more help please send your questions to the fop-users mailing list.