HTML 到 PDF 转换,Base64 图像标签不显示 PDF 中的图像
HTML to PDF conversion, Base64 image tag is not displaying image in PDF
尝试使用核心依赖项 renderer.jar、itext-2.0.8.jar 和 itextpdf-5.5.13.[=25] 将 html 内容转换为 pdf java =] 无法获取图像内容,但 HTML 的其他内容可以 PDF 格式查看。
html 文件包含一个采用 base64 编码的图像标签
<img style = "宽=10px;高=10px;" src="数据:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAARMAAAC3CAMAAAAGjUrGAAAAk1BMV...g==" />
使用 java 函数创建 pdf,该函数接受字符串缓冲区中的 html 内容作为输入并创建 PDF 文件。
public void createReport(StringBuffer sb) {
try {
String tempfile = outputPath + File.separator + "outputPdf.pdf";
PrintWriter writer = new PrintWriter(inputPath, StandardCharsets.UTF_8);
writer.println(sb.toString());
writer.close();
String url = new File(inputPath).toURI().toURL().toString();
OutputStream os = new FileOutputStream(new File(tempfile));
ITextRenderer renderer = new ITextRenderer();
ITextFontResolver resolver = renderer.getFontResolver();
resolver.addFont("C:\Users\DELL\AppData\Local\Microsoft\Windows\Fonts\ANGSA.ttf",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
这适用于飞碟 9.1.20 和 iText 2.1.7。
您应该更新您的依赖项 and/or 检查您的 base64 字符串。
jar是通过maven添加的:
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId>
<version>9.1.20</version>
</dependency>
Maven 依赖项
[INFO] ------------------------< org.example:SO-temp >------------------------
[INFO] Building SO-temp 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:list (default-cli) @ SO-temp ---
[INFO]
[INFO] The following files have been resolved:
[INFO] org.bouncycastle:bctsp-jdk14:jar:1.46:compile
[INFO] org.xhtmlrenderer:flying-saucer-pdf:jar:9.1.20:compile
[INFO] org.xhtmlrenderer:flying-saucer-core:jar:9.1.20:compile
[INFO] com.lowagie:itext:jar:2.1.7:compile
[INFO] org.bouncycastle:bcmail-jdk14:jar:1.64:compile
[INFO] org.bouncycastle:bcpkix-jdk14:jar:1.64:compile
[INFO] org.bouncycastle:bcprov-jdk14:jar:1.64:compile
HTML部分
<html>
<head>
</head>
<body>
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABYAAAAUCAYAAACJfM0wAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9kHFQ83FkONzqIAAAPVSURBVDjLhZVfiFVVFMZ/a58/994Z/0zqWNRLZVReH7LGyLDymhIE0dvpqZGguBGDBNJLD8HxpYgES4UYaUYdLewMPQQ1FIZXkPAhgwyu9CAImY3BiIbjjOM953w9nHOvGpFnsznrnL33t76199rfQsIAk0AJIMGUQEK0EKUtUKsYrgskIcr/3TllQ1OQEBWDAtr1YkILFIMa9L7bpdNuq5drRIwQiaAp0DlIROFNpyBSyfZfXSpAb7IRKEJNiGkgYUjGicrDKPKE/GIeQhJJb2lyGzO1bjJFDYYvltFJnlDwbsIge6s/MXqfODX0lCT2twCdKpnG6hE9LfjoNJzvhVsYwxdBTUzIR6pycs0GDi+/wJgvJvpU3cdGpKriEpMkuhk6MSLqglldWGFHXSduFAVLjrKePSsvMLZMfOYyxvvTJyZYzah8CQARqbeHVSF/vxhIRE0olMqDagKSIVWXHGU9nyz/k3FPjPcrHEMc6ntDooLk0AsFYCRoF+ABeuZBDq74mt393/MFzws5xVhMo9iCk2s2sGflBcY9MYE4sFRM9L8m5GJkktAoOIDERP0s3mbZSg79+g2duZepho2+jM0mzJZtC2M1PPt2YC2//D7JwMy95MENMh9m//6Q4WtHAG+jTMcxeFP4JpwMTT9E5djHi59kIHiEIM3pEM5dqzwt2+HB7mwwtLUzbtEUXj7Igp/hwoD5u8ZeHJl+f8q0AGiE4mkDDrbrFZl7DjIueauodBzZgsPLoOqv2qXYs2PhYzN5/3f4GqQ2B2EIndrnemt6ZMo0vw8wFYDt0nY6vkuTKDtLpMqqK22uz+XkEmRwfWFw+96Bdzgb/kBgi9E1yJZCHhxR86/m5BlkyY58WiZhmAkzIQwTCUYEyZmQq+uG6Nw4gZffIFxWozMLXiBS5XgLHm5RSs0m9enlrTuON4i1k4R1afQz2Lri0gAZZkWOmkCG2QerN3LPH8dI5zNU86kJLIWFACoVyPJEP15+lbcxqyuXkRIbxEXiWvFCVmbF/ZgBxuvrfyO1jMA5PEEKzFZyLMyZDw9v23p5KxUca5Rq2hzQAwVMZsgMAB/gWYAh81qDzGzKqlcI0hVU+uDq7HX6vCqd9D1tyXdiygzLWzK3CXWQEQP1M1hUR0ZSXjmDuN0VIlyM3KMHGGJ86XkODoh97hIjd29GVBTjhByRPCRfXXmV/rP3pLFRyJ01hSdR5csHtvDV44vQS96QsNFCfKzUlKI4xP8DLAmSiJYAxYVunAMRgZLeFvYWxI1btFp3YFyWGNS6vczoVoEvSoq6Z3UH8H8ADlDIyRt9lL4AAAAASUVORK5CYII=" />
</body>
</html>
使用的依赖项:
并参考 link
https://knpcode.com/java-programs/convert-html-to-pdf-in-java-using-flying-saucer-openpdf/
尝试使用核心依赖项 renderer.jar、itext-2.0.8.jar 和 itextpdf-5.5.13.[=25] 将 html 内容转换为 pdf java =] 无法获取图像内容,但 HTML 的其他内容可以 PDF 格式查看。
html 文件包含一个采用 base64 编码的图像标签
<img style = "宽=10px;高=10px;" src="数据:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAARMAAAC3CAMAAAAGjUrGAAAAk1BMV...g==" />
使用 java 函数创建 pdf,该函数接受字符串缓冲区中的 html 内容作为输入并创建 PDF 文件。
public void createReport(StringBuffer sb) {
try {
String tempfile = outputPath + File.separator + "outputPdf.pdf";
PrintWriter writer = new PrintWriter(inputPath, StandardCharsets.UTF_8);
writer.println(sb.toString());
writer.close();
String url = new File(inputPath).toURI().toURL().toString();
OutputStream os = new FileOutputStream(new File(tempfile));
ITextRenderer renderer = new ITextRenderer();
ITextFontResolver resolver = renderer.getFontResolver();
resolver.addFont("C:\Users\DELL\AppData\Local\Microsoft\Windows\Fonts\ANGSA.ttf",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
这适用于飞碟 9.1.20 和 iText 2.1.7。 您应该更新您的依赖项 and/or 检查您的 base64 字符串。
jar是通过maven添加的:
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId>
<version>9.1.20</version>
</dependency>
Maven 依赖项
[INFO] ------------------------< org.example:SO-temp >------------------------
[INFO] Building SO-temp 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:list (default-cli) @ SO-temp ---
[INFO]
[INFO] The following files have been resolved:
[INFO] org.bouncycastle:bctsp-jdk14:jar:1.46:compile
[INFO] org.xhtmlrenderer:flying-saucer-pdf:jar:9.1.20:compile
[INFO] org.xhtmlrenderer:flying-saucer-core:jar:9.1.20:compile
[INFO] com.lowagie:itext:jar:2.1.7:compile
[INFO] org.bouncycastle:bcmail-jdk14:jar:1.64:compile
[INFO] org.bouncycastle:bcpkix-jdk14:jar:1.64:compile
[INFO] org.bouncycastle:bcprov-jdk14:jar:1.64:compile
HTML部分
<html>
<head>
</head>
<body>
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABYAAAAUCAYAAACJfM0wAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9kHFQ83FkONzqIAAAPVSURBVDjLhZVfiFVVFMZ/a58/994Z/0zqWNRLZVReH7LGyLDymhIE0dvpqZGguBGDBNJLD8HxpYgES4UYaUYdLewMPQQ1FIZXkPAhgwyu9CAImY3BiIbjjO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3pLFRyJ01hSdR5csHtvDV44vQS96QsNFCfKzUlKI4xP8DLAmSiJYAxYVunAMRgZLeFvYWxI1btFp3YFyWGNS6vczoVoEvSoq6Z3UH8H8ADlDIyRt9lL4AAAAASUVORK5CYII=" />
</body>
</html>
使用的依赖项:
并参考 link https://knpcode.com/java-programs/convert-html-to-pdf-in-java-using-flying-saucer-openpdf/