In a Java application I am using Saxon HE (9.9) for the XML-FO transformation. Afterwards I am using Apache FOP (2.3) for creating the PDF file. The FOP transformation is slow compared to the execution time on the cli of both transformations subsequently (approx. 12s vs 2s for the FOP part only).
// XML->FO
Processor proc = new Processor(false);
ExtensionFunction highlightingImage = new OverlayImage();
proc.registerExtensionFunction(highlightingImage);
ExtensionFunction mergeImage = new PlanForLandRegisterMainPageImage();
proc.registerExtensionFunction(mergeImage);
ExtensionFunction rolImage = new RestrictionOnLandownershipImage();
proc.registerExtensionFunction(rolImage);
ExtensionFunction fixImage = new FixImage();
proc.registerExtensionFunction(fixImage);
ExtensionFunction decodeUrl = new URLDecoder();
proc.registerExtensionFunction(decodeUrl);
XsltCompiler comp = proc.newXsltCompiler();
XsltExecutable exp = comp.compile(new StreamSource(new File(xsltFileName)));
XdmNode source = proc.newDocumentBuilder().build(new StreamSource(new File(xmlFileName)));
Serializer outFo = proc.newSerializer(foFile);
XsltTransformer trans = exp.load();
trans.setInitialContextNode(source);
trans.setDestination(outFo);
trans.transform();
// FO->PDF
FopFactory fopFactory = FopFactory.newInstance(fopxconfFile);
OutputStream outPdf = new BufferedOutputStream(new FileOutputStream(pdfFile));
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, outPdf);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
Source src = new StreamSource(foFile);
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);
So far I'm pretty sure, that it does not depend on some file handling issues with the produced FO-file. The FO transformation is even slow if I transform a completely different FO file as the one produced with Saxon. Even the output in the console is different when not executing the XML-FO transformation:
Dec 25, 2018 1:54:47 AM org.apache.fop.apps.FOUserAgent processEvent
INFO: Rendered page #1.
Dec 25, 2018 1:54:47 AM org.apache.fop.apps.FOUserAgent processEvent
INFO: Rendered page #2.
This output will not be printed in the console when executing the XML-FO transformation before.
Is there anything in the XML-FO transformation step which has to be closed?
What is the reason for this behaviour?
I think if you use Saxon's own API to set up a Processor
and your extension functions but then want to pipe the transformation XSL-FO result directly to the Apache FOP processor you can directly set up a SAXDestination
:
XsltTransformer trans = exp.load();
trans.setInitialContextNode(source);
FopFactory fopFactory = FopFactory.newInstance(fopxconfFile);
OutputStream outPdf = new BufferedOutputStream(new FileOutputStream(pdfFile));
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, outPdf);
trans.setDestination(new SAXDestination(fop.getDefaultHandler()));
trans.transform();
outPdf.close();
see http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/fop/examples/embedding/java/embedding/ExampleXML2PDF.java?view=markup together with Saxon's http://saxonica.com/html/documentation/javadoc/net/sf/saxon/s9api/XsltTransformer.html#setDestination-net.sf.saxon.s9api.Destination-.