1
votes

I'm trying to convert a Word file into PDF using the Docx4j community package provided on their site. (http://www.docx4java.org/docx4j/docx4j-community-3.3.1.zip)

It looks like they have a version incompatibility between docx4j and FOP on that package, I wonder if anybody hit that problem before and if you know what version of the libraries would make this work.

Here is my code:

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

I copied the stacktrace below, but the error is complaining that FOPFactory does not have the method newInstance() without parameters. This is being called from FORendererApacheFOP.java, I can see that older versions of FOP used to have that method, I tried to replace the version on the package but that breaks other dependencies.

On the package I have Docx4j 3.3.1 and FOP 2.1

Thanks for any help.

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

1

1 Answers

0
votes

The stack trace you are seeing is occuring after an exception has been caught (FOP is unable to initialise FOP v2.1, so is falling back to FOP 1.0 or 1.1 config, which doesn't work, since you have FOP 2.1)

Why can't FOP 2.1 init? The diagnostics we need to see are at https://github.com/plutext/docx4j-export-FO/blob/master/src/main/java/org/docx4j/convert/out/fo/renderers/FORendererApacheFOP.java#L320

[java] } catch (Exception e) { log.warn("Can't set up FOP svn; " + e.getMessage() ); log.debug(e.getMessage(), e); [/java]

So please turn on DEBUG level logging for class org.docx4j.convert.out.fo.renderers.FORendererApacheFOP

To do that, please see the comments at https://github.com/plutext/docx4j/blob/master/src/samples/_resources/log4j.xml

This issue is under active discussion at http://www.docx4java.org/forums/pdf-output-f27/error-on-convert-pdf-with-fo-on-dox4j-3-3-1-t2446.html

But now tracking it as https://github.com/plutext/docx4j-export-FO/issues/1

Does your FOP 2.1 come from Maven, or somewhere else?