当前位置: 首页 > 知识库问答 >
问题:

从带有POI的docx生成pdf时出错

宣滨海
2023-03-14

我想用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.xwpf.converter.xwpfconverterException:org.apache.xmlbeans.impl.values.xmlvalueOutoFrangeException:union value'0000ff”)~[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>

共有1个答案

白腾
2023-03-14

我很难在XDocReport网站和存储库中找到更新的预构建JAR。最后我在Google上搜索了我要找的特定版本号,并在https://mvnrepository.com/artifact/fr.opensagres.xdocreport/fr.opensagres.poi.xwpf.converter.pdf/2.0.1找到了它

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

 类似资料:
  • 使用debian 9.5、Python3.5、libreoffice 5.2、x86_64 arch。 我有一个22页的word文件(docx),其中包含几个图表。 使用bash从终端运行时,以下命令正常工作,即生成22页的pdf文件: 输出: 转换 /tmp/docx5/output.docx- 问题如下:使用subprocess.run从python执行的相同外部命令生成的pdf文件只有一页,

  • 我试图生成一个. docx从超文本标记语言字符串与docx4j在Java,其中包含Base64图像里面。目前,我能够生成Word并下载它(在一个Vaadin项目内),但图像没有正确插入。但是,如果我把超文本标记语言代码放入index.html页面,它们确实显示正确! 我使用的代码是这样的: 有没有什么特别的工作应该做,而我没有做? 编辑: 我现在可以插入图片了!用以下代码替换行下方和行上方的代码:

  • 基于Laravel的应用程序正在将文档(.doc、.docx、.pdf、.png、.otd、html等)转换为pdf,以便将它们全部合并到主pdf文档中。它使用PHPWord和DOMPDF Wrapper等插件的组合来加载和创建文件。每隔一段时间,该进程就会因Word文件而遇到错误。 错误:PhpOffice\PhpWord\Exception\InvalidImageException:无效图像

  • 我正在生成一个。当我尝试运行它时,一个。日志是用以下行生成的:回溯(最近一次调用):文件“cuasimodo.py”,第1077行,在IOError:[Errno 2]中没有这样的文件或目录:“index”。json' 显然index.json是我程序的一个文件,它和其他文件在同一个文件中,但是找到它有一些问题。有什么想法吗?

  • 我正在从一个PDF文档中提取文本。这个PDF是使用一个从AS400读取数据的WS生成的。所以打印文本时,输出是这样的: 和«VS123»,«VS124»是AS400中的变量。Java APi无法从变量中读取值,无法打印变量名而不是变量值。 我用的是PDFBoxhttps://pdfbox.apache.org/提取文本。代码源如下: 输出从以下错误堆栈开始: 避免:字体为ArialMT 2017年

  • 我需要添加表到现有的docx文档,然后转换成Pdf文件,所以我使用Apache POI和Apache POI转换器库。这是我的代码: 但我有一个例外: org.apache.poi.xwpf.converter.core.xwpfConverterException:java.lang.IllegalArgumentException:PdfPTable构造函数中的列数必须大于零。在org.apa