Apache POI异常

4
我需要将docx转换为PDF,我选择使用Apache POI。以下是我的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>

由于某种原因,在转换过程中出现了异常:

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

我通过谷歌搜索尝试找到我缺少的依赖项,至少我认为是这样,但我找不到关于POIXMLDocumentPart的信息,以解决我的问题。
这是我正在使用的转换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) {

        }
    }

对于任何想要使用POI 4.0.x的工作pom.xml的人,这里是:`<dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>fr.opensagres.poi.xwpf.converter.pdf</artifactId> <version>2.0.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.0.1</version> </dependency><!-- 和其他 poi 依赖项 -->` - emrekgn
3个回答

7

XDocReport编译的版本是针对POI 3.17的。 POI 4.0.0有一些更改,因此XDocReport将无法与POI 4.0.0兼容。 POIXMLDocumentPart已移至org.apache.poi.ooxml包中。

请参见https://github.com/opensagres/xdocreport/pull/324

更新(2019年3月):看起来XDocReport 2.0.2已经升级到使用POI 4.0.1。


2
即使使用 apache poi 3.17,也无法与 org.apache.poi.xwpf.converter.pdf 兼容。这是使用 apache poi 3.9 的情况。请参见 https://dev59.com/6FUK5IYBdhLWcg3wwSLg#51337157 和 https://dev59.com/a63la4cB1Zd3GeqPGQvi#51440649。必须使用 fr.opensagres.poi.xwpf.converter.pdf 来配合 apache poi 3.17 使用。 - Axel Richter
1
@AxelRichter,你说得对,我还必须更改xwpf.converter库,之后它才能正常工作。不幸的是,PDF输出有问题,页面数量不正确,图像位置不正确,而且页脚无法添加链接。回去找一个更合适的解决方案。谢谢大家。 - Estevao Santiago
在我的情况下,它使用POI 3.17依赖项和fr.opensagres.xdocreport 2.0.1正常工作。 - AlkaRocks

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'

这两个依赖项足以执行上述示例。

2
我曾经遇到类似的问题,但是我有一个“两步”脚本。
1.从docx模板文档创建docx(用值替换占位符)
2.从创建的docx生成PDF
我面临的问题是两种方法都使用的一些库之间的冲突。当我升级poi-ooxml库版本时,PDF生成器会抛出no class found java.lang.NoClassDefFoundError: org/apache/poi/POIXMLDocumentPart错误,如果版本旧,则docx生成器失败。
对我来说,可以正确配合使用的黄金比例是:
'org.apache.poi',名称:'poi-ooxml',版本:'3.10.1'
'fr.opensagres.xdocreport',名称:'fr.opensagres.xdocreport.converter.docx.xwpf',版本:'1.0.5'
'fr.opensagres.xdocreport',名称:'fr.opensagres.xdocreport.core',版本:'1.0.6'
'fr.opensagres.xdocreport',名称:'org.apache.poi.xwpf.converter.xhtml',版本:'1.0.6'
希望这能帮助某人。我在这个问题上花了几个小时。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接