skip to Main Content

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

        }
    }

3

Answers


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

    Login or Signup to reply.
  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.

    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search