【问题标题】:Error generating pdf from docx with POI使用 POI 从 docx 生成 pdf 时出错
【发布时间】:2023-03-22 12:45:01
【问题描述】:

我想使用 apache poi 将 docx 转换为 pdf,docx 是使用 docx4j 正确生成的。简单文档的转换工作正常,但是当我想转换更风格化的文档时,POI 会抛出异常:

org.apache.xmlbeans.impl.values.XmlValueOutOfRangeException: union value '0000FF">http://schemas.openxmlformats.org/wordprocessingml/2006/main' 15:09:20 org.apache.poi.xwpf.converter.core.XWPFConverterException: org.apache.xmlbeans.impl.values.XmlValueOutOfRangeException: union value '0000FF">http://schemas.openxmlformats.org/wordprocessingml/2006 /主要的' 在 org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:70) ~[org.apache.poi.xwpf.converter.pdf-1.0.6.jar:1.0.6]

有这个异常的原因:

<w:r>
    <w:rPr>
        <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/>
        <w:color w:val="0000FF"><span style="background-color: rgb(51, 153, 102);"><span style="background-color: rgb(255, 0, 0);"><font color="99CC00"/>
        <w:sz w:val="20"/>
        <w:szCs w:val="20"/>
        <w:highlight w:val="red"/>
    </w:rPr>
    <w:t xml:space="preserve">Juillet-Aout</w:t>
</w:r>

这是我的代码:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;


import org.apache.poi.xwpf.usermodel.XWPFDocument;

import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;

public class ConvertDocxPdf
{

    public static void main( String[] args )
    {
        long startTime = System.currentTimeMillis();

        try
        {
            // 1) Load docx with POI XWPFDocument
            InputStream source = new FileInputStream("test.docx");
            XWPFDocument document = new XWPFDocument(source);

            // 2) Convert POI XWPFDocument 2 PDF with iText
            File outFile = new File("result.pdf" );
            outFile.getParentFile().mkdirs();

            OutputStream out = new FileOutputStream( outFile );
            PdfOptions options = null;// PDFViaITextOptions.create().fontEncoding( "windows-1250" );
            PdfConverter.getInstance().convert( document, out, options );
        }
        catch ( Throwable e )
        {
            e.printStackTrace();
        }

        System.out.println( "Generate DocxStructures.pdf with " + ( System.currentTimeMillis() - startTime ) + " ms." );
    }
}

这是导致问题的 XML 行:

<w:r>
    <w:rPr>
        <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/>
        <w:color w:val="0000FF"><span style="background-color: rgb(51, 153, 102);"><span style="background-color: rgb(255, 0, 0);"><font color="99CC00"/> //<-- That line
        <w:sz w:val="20"/>
        <w:szCs w:val="20"/>
        <w:highlight w:val="red"/>
    </w:rPr>
    <w:t xml:space="preserve">Juillet-Aout </w:t>
</w:r>

【问题讨论】:

  • 根据堆栈跟踪,您正在使用旧版本的 XDocs 报告转换器。尝试升级
  • 不明白要升级什么,根据升级我的 Org.apache.poi.xwpf.converter.pdf 是最新版本
  • 你不应该使用那个包名,它是不正确和误导的。最新版本是2.0.1
  • 你在说什么包?只是 Org.apache.poi.xwpf.converter.pdf 还是 Poi?
  • XDocs 报告。不应该有任何 Org.apache.poi.xwpf.converter.pdf 包,因为 Apache POI 没有这样的包。这是 xdocs 报告的错误

标签: pdf docx docx4j xdocreport


【解决方案1】:

我在 XDocReport 网站和存储库中找不到更新的预构建 jar。我最终在谷歌上搜索了我正在寻找的特定版本号,并在https://mvnrepository.com/artifact/fr.opensagres.xdocreport/fr.opensagres.poi.xwpf.converter.pdf/2.0.1 找到了它

我不确定这是否真的回答了这个问题,但它确实回答了如何获取库的更新版本的相关问题。不过,从源代码构建可能更安全。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-10
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 2020-12-13
    相关资源
    最近更新 更多