I am deveveloping a Spring Boot - application which should convert XML to PDF by using XSLT. I would like to use Apache FOP, but it states in its own documentation (https://xmlgraphics.apache.org/fop/2.6/embedding.html)
Apache FOP may currently not be completely thread safe. The code has not been fully tested for multi-threading issues, yet.
Unfortunately the docs don't tell what exactly is not tested yet. Is it possible to convert several independent XMLs into several independent PDFs in paralell (with multiple CPU cores)?
private URI convertToPDF(byte[] body, byte[] xsl, String pdfFileName) throws Exception {
File pdfFile = new File(saveDirectory, pdfFileName);
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
// Setup output
try(BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(pdfFile));) {
// Construct fop with desired output format
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
// Setup XSLT
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(new ByteArrayInputStream(xsl)));
// Setup input for XSLT transformation
Source src = new StreamSource(new ByteArrayInputStream(body));
// Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());
// Start XSLT transformation and FOP processing
transformer.transform(src, res);
return pdfFile.toURI();
}
}
Configuration
public class ConverterConfig {
@Bean
public FopFactory fopFactory() {
return FopFactory.newInstance(new File(".").toURI());
}
}