4

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

        }
    }
Estevao Santiago
  • 793
  • 2
  • 7
  • 30
  • For anyone who is looking for a working pom.xml for POI 4.0.x, here it is: ` fr.opensagres.xdocreport fr.opensagres.poi.xwpf.converter.pdf 2.0.2 org.apache.poi poi 4.0.1 ` – emrekgn Mar 29 '19 at 11:19

3 Answers3

7

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.

PJ Fanning
  • 953
  • 5
  • 13
  • 2
    Even using `apache poi` `3.17` will not work together with `org.apache.poi.xwpf.converter.pdf `. This is using `apache poi` `3.9`. See https://stackoverflow.com/questions/51330192/trying-to-make-simple-pdf-document-with-apache-poi/51337157#51337157 and https://stackoverflow.com/questions/51440312/docx-to-pdf-converter-in-java/51440649#51440649. The `fr.opensagres.poi.xwpf.converter.pdf` must be used for `apache poi` `3.17`. – Axel Richter Sep 15 '18 at 05:26
  • 1
    @AxelRichter you are right, I had to also change thw xwpf.converter library, after that, it worked. Saddely the PDF output is broken, incorrect number of pages, images incorretly positioned and also, can't have links on the footer. Back to find a more suitable solution. Thanks guys. – Estevao Santiago Sep 17 '18 at 14:37
  • In my case, it worked with POI 3.17 dependencies and fr.opensagres.xdocreport 2.0.1 – AlkaRocks Mar 21 '19 at 16:43
2

I had got similar issue, but I have got "two step" script.
1. Create docx from docx template document (replace placeholders with values)
2. Generate PDF from created docx
Problem which I faced is collisions between some libraries which are using by both methods. When I have upgraded poi-ooxml library version -> PDF generator throwed no class found java.lang.NoClassDefFoundError: org/apache/poi/POIXMLDocumentPart and if versions were older, than docx generator failed.
For me golden proportions which worked correctly together was:

'org.apache.poi', name: 'poi-ooxml', version: '3.10.1'
'fr.opensagres.xdocreport', name: 'fr.opensagres.xdocreport.converter.docx.xwpf', version: '1.0.5'
'fr.opensagres.xdocreport', name: 'fr.opensagres.xdocreport.core', version: '1.0.6'
'fr.opensagres.xdocreport', name: 'org.apache.poi.xwpf.converter.xhtml', version: '1.0.6'


Hope it will help someone. I spent on that problem couple hours.

EnGoPy
  • 333
  • 4
  • 14
2
compile group: 'fr.opensagres.xdocreport', name: 'fr.opensagres.poi.xwpf.converter.pdf', version: '2.0.2'
compile group: 'org.apache.poi', name: 'poi-ooxml-schemas', version: '4.1.2'

These two dependencies are enough to execute above example.

Harshad Panmand
  • 410
  • 5
  • 19