这里我尝试将 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 中图像丢失。
尝试改变
renderer.setDocument(newHtmlFile);
到
renderer.setDocument(newHtmlFile, "file:/C:/Users/abc/");