xmlgraphics-fop-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Simon Steiner" <simonsteiner1...@gmail.com>
Subject RE: Generate an areatree file from Java
Date Mon, 28 May 2018 17:44:04 GMT


PDFVTTestCase.java has a example for fo to IF




From: Pieter Masereeuw [mailto:pieter@masereeuw.nl] 
Sent: 28 May 2018 16:08
To: fop-users@xmlgraphics.apache.org
Subject: Generate an areatree file from Java


Hi -

I am trying to create some XMLCalabash (XProc) extensions that work with Fop. One of them
is that I would like to generate an areatree file from a standard FO file.

Although not being excellent in Java, I managed to get this running. But as soon as the FO
file contains an instream-foreign-object, I get a NullPointerException.

If I try do do the same with the Fop command line, everyting works fine, so I think that the
way I invoke Fop from Java is not correct. Maybe I need to pass some handler for SVN objects
to the FopFactoryBuilder, but I neither know how to do that, nor if this is really the right

The problem exists in Fop 2.2 and Fop 2.3. I am using JDK  1.8.

So the question is: how do I use Fop to convert an FO file to an areatree (and maybe an intermediate
file too) from Java?

Below is my Java code plus the top of the stacktrace.

Any help will be greatly appreciated.



ERROR: Error while rendering page 1. Reason: java.lang.NullPointerException
    at org.apache.fop.render.AbstractRenderer.renderXML(AbstractRenderer.java:930) ~[fop.jar:?]
    at org.apache.fop.render.xml.XMLRenderer.renderForeignObject(XMLRenderer.java:785) ~[fop.jar:?]
    at org.apache.fop.render.AbstractRenderer.renderInlineViewport(AbstractRenderer.java:864)
    at org.apache.fop.render.xml.XMLRenderer.renderInlineViewport(XMLRenderer.java:741) ~[fop.jar:?]
    at org.apache.fop.render.AbstractRenderer.renderInlineArea(AbstractRenderer.java:717)
    at org.apache.fop.render.xml.XMLRenderer.renderInlineArea(XMLRenderer.java:722) ~[fop.jar:?]
    at org.apache.fop.render.AbstractRenderer.renderLineArea(AbstractRenderer.java:692) ~[fop.jar:?]


package nl.masereeuw.calabash.steps;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URI;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
import net.sf.saxon.s9api.SaxonApiException;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.FopFactoryBuilder;
import org.apache.fop.apps.MimeConstants;

public class TestAreatree {

    File INPUTFILE_WITH_INSTREAM_FOREIGN_OBJECTS = new File("testareatree-with-instream-foreign-objects.fo");
    File INPUTFILE_WITHOUT_INSTREAM_FOREIGN_OBJECTS = new File("testareatree-without-instream-foreign-objects.fo");

    File OUTPUTFILE = new File("testareatree.xml");
    File FOP_XCONF = new File("fop.xconf");

    public void run() throws SaxonApiException {
        try {
            FopFactory fopFactory;
            if (false) {
                // Under construction, perhaps we should somehow register some kind of handler
for SVG inline-foreign-object elements?
                URI baseUri = null;  // TODO Replace null with correct base-uri, but what
should be its value?
                FopFactoryBuilder fopFactoryBuilder = new FopFactoryBuilder(baseUri);
                Configuration configuration = new DefaultConfigurationBuilder(true).buildFromFile(FOP_XCONF);
                fopFactory = fopFactoryBuilder.build();
            } else {
                fopFactory = FopFactory.newInstance(FOP_XCONF);

            Source source = new StreamSource(INPUTFILE_TO_BE_USED);

            try (OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(OUTPUTFILE)))
                Fop fop = fopFactory.newFop(MimeConstants.MIME_FOP_AREA_TREE, outputStream);

                TransformerFactory factory = TransformerFactory.newInstance();
                Transformer transformer = factory.newTransformer();
                Result result = new SAXResult(fop.getDefaultHandler());
                transformer.transform(source, result);
        } catch (Exception ex) {
            throw new SaxonApiException(ex);

    public static void main(String args[]) {
        try {
            new TestAreatree().run();
        } catch (SaxonApiException ex) {

View raw message