1
votes

I am using Docx4j to first create a .docx file (working as intended), then convert the docx file to pdf using Docx4j.toFO(). The file contains images, and is throwing this stack trace when executed.

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]

Relevant POM.xml entries:

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

Code:

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

1 Answers

0
votes

docx4j currently uses FOP 1.1. (We may make forthcoming docx4j 3.3.0's newly separate docx4j-export-fo project use a more recent FOP over coming days)

So FOP 1.1 ought to work. Otherwise, it sounds like you need to configure an image handler for PNG. Might relate to your xmlgraphics-commons jar?