Saxon XSLT 转换后的慢速 Apache FOP 转换



在Java应用程序中,我使用Saxon HE(9.9)进行XML-FO转换。之后,我使用Apache FOP (2.3)来创建PDF文件。与随后两次转换的 cli 上的执行时间相比,FOP 转换很慢(大约 12 秒,而仅 FOP 部分为 2 秒)。

// 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);        

到目前为止,我很确定,它不依赖于生成的 FO 文件的某些文件处理问题。如果我将完全不同的 FO 文件转换为使用 Saxon 生成的文件,则 FO 转换甚至很慢。不执行 XML-FO 转换时,甚至控制台中的输出也不同:

        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.

之前执行 XML-FO 转换时,此输出不会在控制台中打印。

XML-FO 转换步骤中是否有任何必须关闭的内容?

这种行为的原因是什么?

我认为如果您使用 Saxon 自己的 API 来设置Processor和扩展函数,但随后想将转换 XSL-FO 结果直接传送到 Apache FOP 处理器,您可以直接设置一个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();   

http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/fop/examples/embedding/java/embedding/ExampleXML2PDF.java?view=markup 与撒克逊人的 http://saxonica.com/html/documentation/javadoc/net/sf/saxon/s9api/XsltTransformer.html#setDestination-net.sf.saxon.s9api.Destination 一起看。

最新更新