我使用Docx4j首先创建.docx文件(按预期工作),然后使用Docx4j.toFO()将docx文件转换为pdf。该文件包含图像,并在执行时抛出此堆栈跟踪。
org.docx4j.openpackaging.exceptions.Docx4JException: Exception exporting package
at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:97) ~[docx4j-3.1.0.jar:na]
at org.docx4j.Docx4J.toFO(Docx4J.java:475) ~[docx4j-3.1.0.jar:na]
...
Caused by: java.lang.UnsupportedOperationException: No ImageHandler available for image: file:/usr/tomcat/apache-tomcat-7.0.62/temp/df7fc224-b424-4083-9b85-45032b5ff6e5image1.png (image/png) (org.apache.xmlgraphics.image.loader.impl.ImageRawStream)
at org.apache.fop.render.intermediate.AbstractIFPainter.drawImage(AbstractIFPainter.java:218) ~[fop-1.0.jar:na]
相关的POM.xml条目:
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j</artifactId>
<version>3.2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>fop</artifactId>
<version>2.1</version>
</dependency>
代码:
FOSettings foSettings = Docx4J.createFOSettings();
String outputfilepath = System.getProperty("java.io.tmpdir") + "/form.pdf";
OutputStream os = new java.io.FileOutputStream(outputfilepath);
foSettings.setWmlPackage(wordMLPackage);
if (foSettings.getImageHandler() == null) {
logz.debug("Image handler is null");
foSettings.setImageHandler(foSettings.getImageDirPath() != null ?
new FOConversionImageHandler(foSettings.getImageDirPath(), true) :
new FOConversionImageHandler());
}
Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
答案 0 :(得分:0)
docx4j目前使用FOP 1.1。 (我们可能会制作即将发布的docx4j 3.3.0和新单独的docx4j-export-fo项目在未来几天内使用更新的FOP)
所以FOP 1.1应该有效。否则,听起来您需要为PNG配置图像处理程序。可能与你的xmlgraphics-commons jar有关吗?