飞碟不显示pdf中的图像

问题描述 投票:0回答:1

这里我尝试将 html 转换为 pdf,但 html 中可用的图像未显示在 pdf 中。

pom.xml

<dependency>
    <groupId>org.xhtmlrenderer</groupId>
    <artifactId>flying-saucer-core</artifactId>
    <version>9.1.20</version>
</dependency>

<dependency>
    <groupId>org.xhtmlrenderer</groupId>
    <artifactId>flying-saucer-pdf-openpdf</artifactId>
    <version>9.1.20</version>
</dependency

html

                <img id="proof" src="$id1" onerror="this.style.display='none'" /> 
                <img id="proof" src="$id2" onerror="this.style.display='none'" />
                <img id="proof" src="$id3" onerror="this.style.display='none'" />
                <img id="proof" src="$id4" onerror="this.style.display='none'" />
                <img id="proof" src="$id5" onerror="this.style.display='none'" />

我正在将 $id1、$id2... 等转换为路径并将其添加到临时文件

htmlString = htmlString.replace("$id1", "C:\\Users\\abc\\1.jpeg");

File newHtmlFile = File.createTempFile(fileName, ".html", dir);
FileUtils.writeStringToFile(newHtmlFile, htmlString, Charset.forName("UTF-8"));


                System.out.println("About to convert html to pdf");
                ITextRenderer renderer = new ITextRenderer();
                renderer.setDocument(newHtmlFile);
                renderer.layout();
                OutputStream outputStream = new FileOutputStream(dest);
                renderer.createPDF(outputStream);
                outputStream.close();
                System.out.println("Done");

html 正确显示图像,但 pdf 中图像丢失。

java html html-to-pdf flying-saucer
1个回答
0
投票

尝试改变

renderer.setDocument(newHtmlFile);

renderer.setDocument(newHtmlFile, "file:/C:/Users/abc/");
© www.soinside.com 2019 - 2024. All rights reserved.