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

Error when converting docx to PDF with Docx4j and FOP

我正在尝试使用他们网站上提供的 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) at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:119) at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:168) at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:47) at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:82) at org.docx4j.Docx4J.toFO(Docx4J.java:568) at org.docx4j.convert.out.fo.FOPAreaTreeHelper.getAreaTreeViaFOP(FOPAreaTreeHelper.java:191) at 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) at java.lang.reflect.Method.invoke(Method.java:497) at org.apache.xalan.extensions.ExtensionHandlerJavaPackage.callFunction(ExtensionHandlerJavaPackage.java:343) at org.apache.xalan.extensions.ExtensionHandlerJavaPackage.callFunction(ExtensionHandlerJavaPackage.java:440) at org.apache.xalan.extensions.ExtensionsTable.extFunction(ExtensionsTable.java:222) at org.apache.xalan.transformer.TransformerImpl.extFunction(TransformerImpl.java:475) at org.apache.xpath.functions.FuncExtFunction.execute(FuncExtFunction.java:208) at org.apache.xpath.XPath.execute(XPath.java:337) at org.apache.xalan.templates.ElemCopyOf.execute(ElemCopyOf.java:134) at org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(TransformerImpl.java:2402) at org.apache.xalan.templates.ElemLiteralResult.execute(ElemLiteralResult.java:1376) at org.apache.xalan.templates.ElemApplyTemplates.transformSelectedNodes(ElemApplyTemplates.java:395) at 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) at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1275) at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1253) at org.docx4j.XmlUtils.transform(XmlUtils.java:1275) at org.docx4j.XmlUtils.transform(XmlUtils.java:1100) at org.docx4j.convert.out.common.AbstractXsltExporterDelegate.process(AbstractXsltExporterDelegate.java:66) at org.docx4j.convert.out.common.AbstractWmlExporter.process(AbstractWmlExporter.java:63) at org.docx4j.convert.out.common.AbstractWmlExporter.process(AbstractWmlExporter.java:32) at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:79) at org.docx4j.Docx4J.toFO(Docx4J.java:568) at 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) at java.lang.reflect.Method.invoke(Method.java:497) at 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) at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:119) at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:168) at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:47) at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:82) at org.docx4j.Docx4J.toFO(Docx4J.java:568) at 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) at java.lang.reflect.Method.invoke(Method.java:497) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) org.docx4j.openpackaging.exceptions.Docx4JException: Exception exporting package at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:109) at org.docx4j.Docx4J.toFO(Docx4J.java:568) at 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) at java.lang.reflect.Method.invoke(Method.java:497) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) Caused by: java.lang.NullPointerException at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:199) at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:159) at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:168) at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:47) at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:82) ... 7 more

您看到的堆栈跟踪是在捕获到异常后发生的(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] } 赶上(异常 e){ log.warn("Can't set up FOP svn; " + e.getMessage() ); log.debug(e.getMessage(), e); [/java]

所以请为 class 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 还是其他地方?