使用Docx4j和FOP将docx转换为PDF时出错

时间:2016-10-10 22:01:57

标签: apache-fop docx4j

我正在尝试使用其网站上提供的Docx4j社区包将Word文件转换为PDF。 (http://www.docx4java.org/docx4j/docx4j-community-3.3.1.zip

看起来他们在该软件包上的docx4j和FOP之间存在版本不兼容,我想知道是否有人之前遇到过这个问题,如果你知道哪个版本的库会使这个工作。

这是我的代码:

FOSettings foSettings = Docx4J.createFOSettings();
String inputfilepath = "path/to/file.docx";
String outputfilepath = "path/to/file.pdf";

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
FileOutputStream os = new java.io.FileOutputStream(outputfilepath);

foSettings.setFoDumpFile(new java.io.File(inputfilepath + ".fo"));
foSettings.setWmlPackage(wordMLPackage);

Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);

我复制了下面的堆栈跟踪,但错误是抱怨FOPFactory没有没有参数的方法newInstance()。 这是从FORendererApacheFOP.java调用的,我可以看到旧版本的FOP曾经有过这个方法,我试图替换包上的版本,但这会打破其他依赖。

在包装上我有Docx4j 3.3.1和FOP 2.1

感谢您的帮助。

  

java.lang.NoSuchMethodException:org.apache.fop.apps.FopFactory.newInstance()       at java.lang.Class.getDeclaredMethod(Class.java:2130)       at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.createFopFactory(FORendererApacheFOP.java:329)       at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.getFopFactory(FORendererApacheFOP.java:253)       在org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:119)       在org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:168)       在org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:47)       在org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:82)       在org.docx4j.Docx4J.toFO(Docx4J.java:568)       在org.docx4j.convert.out.fo.FOPAreaTreeHelper.getAreaTreeViaFOP(FOPAreaTreeHelper.java:191)       在org.docx4j.convert.out.fo.LayoutMasterSetBuilder.fixExtents(LayoutMasterSetBuilder.java:138)       at org.docx4j.convert.out.fo.LayoutMasterSetBuilder.getLayoutMasterSetFragment(LayoutMasterSetBuilder.java:97)       at org.docx4j.convert.out.fo.XsltFOFunctions.getLayoutMasterSetFragment(XsltFOFunctions.java:81)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       在java.lang.reflect.Method.invoke(Method.java:497)       在org.apache.xalan.extensions.ExtensionHandlerJavaPackage.callFunction(ExtensionHandlerJavaPackage.java:343)       在org.apache.xalan.extensions.ExtensionHandlerJavaPackage.callFunction(ExtensionHandlerJavaPackage.java:440)       在org.apache.xalan.extensions.ExtensionsTable.extFunction(ExtensionsTable.java:222)       在org.apache.xalan.transformer.TransformerImpl.extFunction(TransformerImpl.java:475)       在org.apache.xpath.functions.FuncExtFunction.execute(FuncExtFunction.java:208)       在org.apache.xpath.XPath.execute(XPath.java:337)       在org.apache.xalan.templates.ElemCopyOf.execute(ElemCopyOf.java:134)       at org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(TransformerImpl.java:2402)       在org.apache.xalan.templates.ElemLiteralResult.execute(ElemLiteralResult.java:1376)       at org.apache.xalan.templates.ElemApplyTemplates.transformSelectedNodes(ElemApplyTemplates.java:395)       在org.apache.xalan.templates.ElemApplyTemplates.execute(ElemApplyTemplates.java:178)       at org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(TransformerImpl.java:2402)       at org.apache.xalan.transformer.TransformerImpl.applyTemplateToNode(TransformerImpl.java:2272)       at org.apache.xalan.transformer.TransformerImpl.transformNode(TransformerImpl.java:1358)       at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:711)       在org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1275)       在org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1253)       在org.docx4j.XmlUtils.transform(XmlUtils.java:1275)       在org.docx4j.XmlUtils.transform(XmlUtils.java:1100)       在org.docx4j.convert.out.common.AbstractXsltExporterDelegate.process(AbstractXsltExporterDelegate.java:66)       在org.docx4j.convert.out.common.AbstractWmlExporter.process(AbstractWmlExporter.java:63)       在org.docx4j.convert.out.common.AbstractWmlExporter.process(AbstractWmlExporter.java:32)       在org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:79)       在org.docx4j.Docx4J.toFO(Docx4J.java:568)       在PDFConversion.main(PDFConversion.java:26)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       在java.lang.reflect.Method.invoke(Method.java:497)       在com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)   java.lang.NoSuchMethodException:org.apache.fop.apps.FopFactory.newInstance()       at java.lang.Class.getDeclaredMethod(Class.java:2130)       at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.createFopFactory(FORendererApacheFOP.java:329)       at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.getFopFactory(FORendererApacheFOP.java:253)       在org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:119)       在org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:168)       在org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:47)       在org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:82)       在org.docx4j.Docx4J.toFO(Docx4J.java:568)       在PDFConversion.main(PDFConversion.java:26)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       在java.lang.reflect.Method.invoke(Method.java:497)       在com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)   org.docx4j.openpackaging.exceptions.Docx4JException:异常导出包       在org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:109)       在org.docx4j.Docx4J.toFO(Docx4J.java:568)       在PDFConversion.main(PDFConversion.java:26)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       在java.lang.reflect.Method.invoke(Method.java:497)       在com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)   引起:java.lang.NullPointerException       在org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:199)       在org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:159)       在org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:168)       在org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:47)       在org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:82)       ......还有7个

1 个答案:

答案 0 :(得分:0)

您遇到的堆栈跟踪是在捕获到异常后发生的(FOP无法初始化FOP v2.1,因此回落到FOP 1.0或1.1配置,这不起作用,因为您有FOP 2.1 )

为什么不能FOP 2.1初始化?我们需要看到的诊断信息是https://github.com/plutext/docx4j-export-FO/blob/master/src/main/java/org/docx4j/convert/out/fo/renderers/FORendererApacheFOP.java#L320

【JAVA]         } catch(例外e){             log.warn(“无法设置FOP svn;”+ e.getMessage());             log.debug(e.getMessage(),e); [/ JAVA]

因此,请为类org.docx4j.convert.out.fo.renderers.FORendererApacheFOP启用DEBUG级别日志记录

为此,请参阅https://github.com/plutext/docx4j/blob/master/src/samples/_resources/log4j.xml

上的评论

此问题正在http://www.docx4java.org/forums/pdf-output-f27/error-on-convert-pdf-with-fo-on-dox4j-3-3-1-t2446.html

进行积极讨论

但现在将其跟踪为https://github.com/plutext/docx4j-export-FO/issues/1

你的FOP 2.1是来自Maven还是其他地方?