微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

对于不同的文档,Apache FOP 线程安全吗

如何解决对于不同的文档,Apache FOP 线程安全吗

我正在开发一个 Spring Boot - 应用程序,它应该使用 XSLT 将 XML 转换为 PDF。 我想使用 Apache FOP,但它在自己的文档 (https://xmlgraphics.apache.org/fop/2.6/embedding.html) 中声明

Apache FOP 目前可能不是完全线程安全的。该代码尚未针对多线程问题进行全面测试。

不幸的是,文档并没有说明尚未测试的内容。是否可以将多个独立的 XML 并行转换为多个独立的 PDF(具有多个 cpu 内核)?

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());
        }
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。