I need to convert a docx to a PDF and I am going with Apache POI. This is my POM:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
<version>1.0.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.0.0</version>
</dependency>
</dependencies>
For some reason, I am getting an exception during when the conversion is running:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/POIXMLDocumentPart at org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument.getFontsDocument(XWPFStylesDocument.java:1477) at org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument.(XWPFStylesDocument.java:190) at org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument.(XWPFStylesDocument.java:184) at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.createStylesDocument(XWPFDocumentVisitor.java:166) at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.(XWPFDocumentVisitor.java:159) at org.apache.poi.xwpf.converter.pdf.internal.PdfMapper.(PdfMapper.java:149) at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:55) at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:38) at org.apache.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:45) at temp.main.Teste(main.java:30) at temp.main.main(main.java:18) Caused by: java.lang.ClassNotFoundException: org.apache.poi.POIXMLDocumentPart at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 11 more
I googled trying to find what is the dependency I missing, at least I think that's the case, but I can't find information about POIXMLDocumentPart that is able to fix my issue.
This is the method i am using to convert the docx:
public static void Teste(File file, String destino) {
try {
InputStream doc = new FileInputStream(file);
XWPFDocument document = new XWPFDocument(doc);
PdfOptions options = PdfOptions.create();
OutputStream out = new FileOutputStream(new File(destino));
PdfConverter.getInstance().convert(document, out, options);
new File(destino);
} catch(Exception e) {
}
}
XDocReport is compiled against POI 3.17. POI 4.0.0 has some changes and XDocReport will not work with POI 4.0.0. POIXMLDocumentPart moved to the package org.apache.poi.ooxml.
See https://github.com/opensagres/xdocreport/pull/324
Update (March 2019): Looks XDocReport 2.0.2 has been updated to use POI 4.0.1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With