概述
一、背景
业务中需要将网页或者其他途径获取的HTML格式文件转换成PDF返回给前端做打印,如果直接返回HTML文件会存在设备兼容问题,所以后端统一处理成PDF文件返回;
遇到的问题:
a)中文字体不显示;
b)特殊符号转义失败;
二、具体实现
1、用到的依赖
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>html2pdf</artifactId>
<version>4.0.2</version>
</dependency>
2、代码示例
package com.*.*.service;
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.io.util.ResourceUtil;
import com.itextpdf.io.util.StreamUtil;
import com.itextpdf.layout.font.FontProvider;
import java.io.*;
import java.util.Calendar;
/**
* className: HtmlToPdfTest
* description: HTML转PDF代码示例
*/
public class HtmlToPdfTest {
public static void main(String[] args) {
//这里我测试是直接拿的字符串,也可以直接从文件里面读,依赖里面有直接通过输入输出流转换的工具方法
String html = "<div style="position: relative;width: 100mm;height: 100mm;font-family: 黑体;">n" +
" <div style="position: absolute;top: 1mm;right: 34mm;width: 8mm;height: 7mm;line-height: 7mm;box-sizing:border-box;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;border: 1px solid #000000;text-align: center;font-size: 10.5pt;font-weight: bold;font-family: 黑体;">n" +
" 1/1n" +
" </div>n" +
" <div style="position: absolute;top: 4mm;right: 2mm;width: 32mm;height: 4mm;line-height: 4mm;text-align: left;font-size: 8pt;font-family: 黑体;overflow: hidden;white-space: nowrap;">n" +
" 单号<span style="font-weight: bold;">20220710833314</span>n" +
" </div>n" +
" <div id="cysDiv" style="position: absolute;top: 8mm;left: 3mm;width: 29mm;height: 7mm;line-height: 7mm;font-size: 15pt;font-family: 黑体;">n" +
" 跑得快快递n" +
" </div>n" +
" <div id="ydDiv" style="position: absolute;top: 15mm;left: 3mm;width: 8mm;height: 5mm;line-height: 5mm;font-size: 9pt;font-family: 黑体;">n" +
" 运单n" +
" </div>n" +
" <div id="ydhDiv" style="position: absolute;top: 15mm;left: 11mm;width: 47mm;height: 5mm;line-height: 5mm;font-size: 9pt;font-family: 黑体;">n" +
" TEST1884552126n" +
" </div>n" +
" <div id="khyqDiv" style="position: absolute;top: 8mm;left: 32mm;width: 26mm;height: 7mm;font-size: 8pt;font-family: 黑体;">n" +
" 送货时间不限n" +
" </div>n" +
" <div id="ddhtmDiv" style="position: absolute;top: 9mm;right: 2mm;width: 40mm;height: 7mm;text-align: center;">n" +
" <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHEAAAAcAQAAAAB+b235AAAACXBIWXMAAE69AABOvQFzamgUAAAAEnRFWHRTb2Z0d2FyZQBCYXJjb2RlNEryjnYuAAAAHklEQVR42mPQSTt6dvPmsrbr1gaThLsYGEb5Q4oPAG8dwwVvdZr5AAAAAElFTkSuQmCC"/>n" +
" </div>n" +
" <div id="ddhDiv" style="position: absolute;top: 16mm;right: 2mm;width: 40mm;height: 4mm;line-height: 4mm;font-size: 9pt;text-align: center;">n" +
" 20220710833314n" +
" </div>n" +
" <div id="sDiv" style="position: absolute;top: 20mm;left: 0mm;width: 10mm;height: 15mm;line-height: 15mm;text-align: center;font-size: 14pt;font-family: 黑体;box-sizing:border-box;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;border-bottom: 1px solid #000000;border-right: 1px solid #000000;">n" +
" 收n" +
" </div>n" +
" <div id="sjrxxDiv" style="position: absolute;top: 20mm;left: 10mm;width: 90mm;height: 5mm;padding-left: 2mm;font-size: 12pt;font-weight: bold;font-family: 黑体;box-sizing:border-box;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;">n" +
" 米奇 888*****390,888*****390n" +
" </div>n" +
" <div id="sjrdzDiv" style="position: absolute;top: 25mm;left: 10mm;width: 90mm;height: 10mm;padding-left: 2mm;padding-right: 2mm;font-family: 黑体;overflow: hidden;box-sizing:border-box;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;border-bottom: 1px solid #000000;">n" +
" <span style="font-size: 12pt;font-weight: bold;">江苏省.南通市.启东市</span><span style="font-size: 10pt;">.米奇妙妙屋</span>n" +
" </div>n" +
" <div id="destRouteDiv" style="position: absolute;top: 35mm;left: 3mm;width: 70mm;height: 20mm;line-height: 20mm;white-space:nowrap;overflow: hidden;font-size: 23pt;font-weight: bold;font-family: 黑体;">n" +
" 513TA-T77-013n" +
" </div>n" +
" <div id="ydhtmDiv" style="position: absolute;top: 55mm;left: 1mm;width: 70mm;height: 15mm;">n" +
" <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOoAAAA4AQAAAADbGVnHAAAACXBIWXMAAE69AABOvQFzamgUAAAAEnRFWHRTb2Z0d2FyZQBCYXJjb2RlNEryjnYuAAAAOElEQVR42mOQnn9E+vnxmwePHWRsPOdh+LH5bR9/vnyzfHq7xQ81BoZR6VHpUelR6VHpUenBLA0AFgsz1nDCSaIAAAAASUVORK5CYII="/>n" +
" </div>n" +
" <div id="ydhtmwzDiv" style="position: absolute;top: 70mm;left: 20mm;width: 40mm;height: 8mm;font-size: 9pt;font-family: 黑体;">n" +
" <div style="line-height: 4mm;">运单 TEST1884552126</div>n" +
" </div>n" +
" <div id="rgysmDiv" style="position: absolute;top: 70mm;left: 3mm;width: 15mm;height: 8mm;line-height: 8mm;font-size: 27pt;font-family: 黑体;overflow: hidden;">n" +
" n" +
" </div>n" +
" <div id="jitxLabelDiv" style="position: absolute;top: 36mm;left: 61mm;width: 8mm;height: 4mm;line-height: 4mm;font-size: 9pt;overflow: hidden;font-family: 黑体;font-weight: bold;">n" +
" JITXn" +
" </div>n" +
" <div id="qrCodeDiv" style="position: absolute;top: 36mm;left: 70mm;width: 28mm;height: 28mm;">n" +
" <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHAAAABwCAIAAABJgmMcAAAClElEQVR42u3dXW4DIQxG0ex/09MFRGrB3zUx6eUxmjbMobLwj9TXK1vP21p55v3hrU9++a6Vh1c+KWu8BBX0EtBnZ61sJVSrHefWfrYOb1FDUEGvA115gTBU1VYYTMOYvvibBRX0n4CGAY5CD0OwoIIKml8vtl4A/3H8D0VQQb8MNMyUwjgblmbwpO7zqaeggvaAhvXQL/vk8wVmQQXtAX36F5UX9YVp8mUFFXQ2aBgx8UYF1SsNt1puxwoq6HBQMElAGh5UvSMsc5RjsaCCDgcNI12tWBlegKgxrI6bmaCCDgelwiL+bh0tyQP5nqCC3gtKxbUD5RL8yMtJlKCCDgfti3Qnyxzh5ATYxBVU0FtA8U5DXxQLzxXPyv5IPQUVdBLogdtSX800rNrgSZSggl4EemBWgApV1JQSvlVBBZ0PWouY+UBAUmntK47kGxNU0HtB+6oSlCOVjIEFX0EFvRe0r/54ssOKZ3erLRBBBb0WFM9n+vKi2nhEzZq8hwoq6ClQqs8R3nLwK1EtKwsnqwQV9EZQfEAhHDXo63pSMXS76ymooJ8DDQMc3nvoOwb821czJUEFnQ3aV3EAQxVSf+nojAoq6HWgHfkD0lyhijVUVXf72iSooCNBwwZD33xD7fqFnxBQYBZU0JGgeDDFacJZCiqJ2p4cEVTQkaDUVQacHGIf7sgJBRV0OOgDrb4yB3VpCxOk1XqooILOBu2LLOEwxIHGCd7jEVTQi0CpFmBfHoL3XMMazWqmJKigs0FrW9naLt6xOFn0FFRQQZ/O/wEWngc+HkFOjggq6P2gYcWBOk6qJrIb0wUV9DrQA9MDVKYUdj2pTkxL6imooM2gVD00bJyE1x1q9KG8Z0EFHQ76uND1A8r/5w17MeJxAAAAAElFTkSuQmCC" width="100%" height="100%"/>n" +
" </div>n" +
" <div id="proCodeDiv" style="position: absolute;top: 64mm;left: 75mm;width: 20mm;height: 20mm;">n" +
" <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADkAAAA5CAYAAACMGIOFAAAACXBIWXMAAC4jAAAuIwF4pT92AAAKTWlDQ1BQaG90b3Nob3AgSUNDIHByb2ZpbGUAAHjanVN3WJP3Fj7f92UPVkLY8LGXbIEAIiOsCMgQWaIQkgBhhBASQMWFiApWFBURnEhVxILVCkidiOKgKLhnQYqIWotVXDjuH9yntX167+3t+9f7vOec5/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bWlnKQjY2NWEiz2Uzs293djfWtqan5MD5kkIeHh0itVotORKVS+eXMV8LhcKD09HQs5OTkpHKQvb292IlUV1cT+46NjWF9dTodYlk2+Aq6lPB4PDA5OYnV29raiL0XFxexWnNzM0RGRgbfC5ESU1NTAa+20+kk8nW73YhhGFFfrVaLDg4OvtYLkRKBfsXGxkaIjo4m8l1fX4fz83NRzWg0Qm5urjKFrIeHB1heXsbqTU1NxN4zMzNYLS8vT7lq3crKCjw9PYlqqampUFpaSuTL8zwsLCxgdZPJpBykxWLBahUVFRATE0Pka7Va4eTkRFTTaDRQU1OjDCTP87CysoLV6+vrib2Xl5exZZf8/HxgGEYZyKOjIzg9PRXVwsLCoKysLCRbR2VlZcA6rayQGxsb4PP5RDWGYSAnJ4fI9/n5GXZ3dwNCKtYmCHSrFhUVEddUbTYb3N/fY+u1hYWFykDyPA87OztYvbi4mNh7a2sLq2VlZYFOp1MG8v7+Hq6urrB6QUEBsTeuhvO+6Hz2NphskA8PD9hKW2xsLPY0EswdYrVasbrRaPy8LCkX5NXVFXbRyc7OhpSUFCLfy8tLuLi4wOoGg0E5yMPDQ6zGMAxxK+7s7Ax4nhfV1Go1ZGRkKAd5fHyM1bKzs0Pim5CQENQdIhsk7ulAagsAd7h4r7xHRUUpA/n29hZwZSV9tHrPyUCQwaSBLJAsy2I3aymQCCG4vb3F6qmpqcE1feSAfHp6ApZlsTppV5rjOHh8fJScBrJA2u32gC/1vrfKSRpLgbpncXFxykGyLBuw+0wK6Xa7wePxSE4DWSA/ewdWCmQg72D/JCDbXwCSk5OxK52UNzrS0tJE2/e/39EJIn4NANHkCW0u0ht+AAAAAElFTkSuQmCC"/>n" +
" </div>n" +
" <div id="ybzPicDiv" style="position: absolute;top: 72mm;left: 60mm;width: 7mm;height: 7mm;line-height: 7mm;font-size: 10.5pt;font-weight: bold;text-align: center;display: none;">n" +
" <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAACGElEQVRIS73WS6hNcRTH8c/1yGNg4pW8JkwoBsiIMqAMUJRQysQjpSgxRDeU58DEQFEm5JEYeUwUeSQmGKCQMpAYYeLVuv3/t223z2nfc/fxn5z67/3/f89ae63f+vX4z6tngLwhmIgp+ImP+IQ/de+pAxyO1ViD6XiH94j9AMfea1zCDfxqB28HjGi2Yieu4BxetbhsLjZhGY6mdytfbQUckyARyR58qZmySTiJkdiA7+VzVcDReITzOFITVH7tMJZiEX4UH1YBL+I5ejuE5WP7MB+r8DtvloFrsRErBgnLx8/gPs5WAaPqnmIJPjcEHIu7mJOjLEa4GRNwsCFYvibq4DEux0YReAtbUp81yVyIXVhXBI7CE8xukpTuiqBCLEIg+iOciWOporrAdA8ro59zSqNf1mN7N2iIVjuAlxm4OCnDti4BQ2f340UGzsDxLqc0evtrBob2RQ/O6kKElUUTnJuIlL5tGLogtUWI+T99GI0/DiG8Ta4YVw9wtQyMXnyGefjWEHEqriUR73MFZfHejfHY2wBwKO4kqYzfvlUGhoDfxgWcHiT0BMI1hGPoX1XzMKb9wzRSIv+drENp6oSghNlqC4yH2WKEBkZ661qMmDanMGIgFiP/m0hHSN2OmiYqBvfyTk1UMQvxXcMihlUMW/gh2cRhiCqchjfJJl4v2omqb1HHlxbPZSM8OfnPrhjhToqm5Zm//v5hHb53H40AAAAASUVORK5CYII="/>n" +
" </div>n" +
" <div id="ybzDiv" style="position: absolute;top: 72mm;left: 60.5mm;width: 7mm;height: 7mm;line-height: 7mm;font-size: 10.5pt;font-weight: bold;text-align: center;display: none;">n" +
" 预n" +
" </div>n" +
" <div id="hhPicDiv" style="position: absolute;top: 72mm;left: 68mm;width: 7mm;height: 7mm;line-height: 7mm;font-size: 10.5pt;font-weight: bold;text-align: center;display: none;">n" +
" <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAACGElEQVRIS73WS6hNcRTH8c/1yGNg4pW8JkwoBsiIMqAMUJRQysQjpSgxRDeU58DEQFEm5JEYeUwUeSQmGKCQMpAYYeLVuv3/t223z2nfc/fxn5z67/3/f89ae63f+vX4z6tngLwhmIgp+ImP+IQ/de+pAxyO1ViD6XiH94j9AMfea1zCDfxqB28HjGi2Yieu4BxetbhsLjZhGY6mdytfbQUckyARyR58qZmySTiJkdiA7+VzVcDReITzOFITVH7tMJZiEX4UH1YBL+I5ejuE5WP7MB+r8DtvloFrsRErBgnLx8/gPs5WAaPqnmIJPjcEHIu7mJOjLEa4GRNwsCFYvibq4DEux0YReAtbUp81yVyIXVhXBI7CE8xukpTuiqBCLEIg+iOciWOporrAdA8ro59zSqNf1mN7N2iIVjuAlxm4OCnDti4BQ2f340UGzsDxLqc0evtrBob2RQ/O6kKElUUTnJuIlL5tGLogtUWI+T99GI0/DiG8Ta4YVw9wtQyMXnyGefjWEHEqriUR73MFZfHejfHY2wBwKO4kqYzfvlUGhoDfxgWcHiT0BMI1hGPoX1XzMKb9wzRSIv+drENp6oSghNlqC4yH2WKEBkZ661qMmDanMGIgFiP/m0hHSN2OmiYqBvfyTk1UMQvxXcMihlUMW/gh2cRhiCqchjfJJl4v2omqb1HHlxbPZSM8OfnPrhjhToqm5Zm//v5hHb53H40AAAAASUVORK5CYII="/>n" +
" </div>n" +
" <div id="hhDiv" style="position: absolute;top: 72mm;left: 68.5mm;width: 7mm;height: 7mm;line-height: 7mm;font-size: 10.5pt;font-weight: bold;text-align: center;display: none;">n" +
" 换n" +
" </div>n" +
" <div id="goodsDiv" style="position: absolute;top: 80mm;left: 0mm;width: 66mm;height: 20mm;font-size: 7.5pt;padding-left: 2mm;overflow: hidden;font-family: 黑体;box-sizing:border-box;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;border-top: 1px solid #000000;border-right: 1px solid #000000;">n" +
" <div>88888160-BLK12*黑色*12*1</div>n" +
" </div>n" +
" <div id="dyDiv" style="position: absolute;top: 80mm;left: 66mm;width: 6mm;height: 6mm;line-height: 3mm;text-align: center;font-size: 7pt;overflow: hidden;font-family: 黑体;box-sizing:border-box;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;border-top: 1px solid #000000;">n" +
" 打印时间n" +
" </div>n" +
" <div id="dyrqDiv" style="position: absolute;top: 80mm;left: 72mm;width: 15mm;height: 3mm;line-height: 3mm;text-align: center;font-size: 7pt;overflow: hidden;font-family: 黑体;box-sizing:border-box;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;border-top: 1px solid #000000;">n" +
" 2020-08-07n" +
" </div>n" +
" <div id="dysjDiv" style="position: absolute;top: 83mm;left: 72mm;width: 15mm;height: 3mm;line-height: 3mm;text-align: center;font-size: 7pt;font-weight: bold;overflow: hidden;font-family: 黑体;">n" +
" 20:17:31n" +
" </div>n" +
" <div id="yajDiv" style="position: absolute;top: 80mm;left: 87mm;width: 13mm;height: 3mm;line-height: 3mm;padding-left: 1mm;font-size: 7pt;overflow: hidden;font-family: 黑体;box-sizing:border-box;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;border-top: 1px solid #000000;border-left: 1px solid #000000;">n" +
" 已安检n" +
" </div>n" +
" <div id="yysDiv" style="position: absolute;top: 83mm;left: 87mm;width: 13mm;height: 3mm;line-height: 3mm;padding-left: 1mm;font-size: 7pt;overflow: hidden;font-family: 黑体;box-sizing:border-box;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;border-left: 1px solid #000000;">n" +
" 已验视n" +
" </div>n" +
" <div id="qsDiv" style="position: absolute;top: 86mm;left: 66mm;width: 6mm;height: 6mm;line-height: 6mm;text-align: center;font-size: 7pt;overflow: hidden;font-family: 黑体;box-sizing:border-box;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;border-top: 1px solid #000000;border-bottom: 1px solid #000000;">n" +
" 签收n" +
" </div>n" +
" <div id="qskkDiv" style="position: absolute;top: 86mm;left: 72mm;width: 28mm;height: 6mm;box-sizing:border-box;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;border-top: 1px solid #000000;border-bottom: 1px solid #000000;">n" +
"n" +
" </div>n" +
" <div id="dhDiv" style="position: absolute;top: 92mm;left: 66mm;width: 6mm;height: 4mm;line-height: 4mm;text-align: center;font-size: 7pt;overflow: hidden;font-family: 黑体;">n" +
" 单号n" +
" </div>n" +
" <div id="dhzDiv" style="position: absolute;top: 92mm;left: 72mm;width: 26mm;height: 4mm;line-height: 4mm;font-size: 7.5pt;overflow: hidden;font-family: 黑体;">n" +
" 20220710833314n" +
" </div>n" +
" <div id="cywlxDiv" style="position: absolute;top: 96mm;left: 66mm;width: 32mm;height: 4mm;line-height: 4mm;font-size: 9pt;overflow: hidden;font-weight: bold;font-family: 黑体;">n" +
" n" +
" </div>n" +
"</div>n";
try(FileOutputStream outputStream = new FileOutputStream("D:\table.pdf")){
Long start = Calendar.getInstance().getTimeInMillis();
System.out.println("开始时间--" + start);
//替换掉文档里面的转义字符,工具无法转换转义符,页面会乱码,所以不建议直接通过流转换成PDF,而是先读到内存里替换掉转义符再转
html = html.replace(" ", " ");
html = html.replace("–", "–");
html = html.replace("—", "—");
html = html.replace("‘", "‘");
html = html.replace("’", "’");
html = html.replace("‚", "‚");
html = html.replace("“", "“");
html = html.replace("”", "”");
html = html.replace("„", "„");
html = html.replace("′", "′");
html = html.replace("″", "″");
html = html.replace("‹", "‹");
html = html.replace("›", "›");
html = html.replace("‾", "‾");
ConverterProperties converterProperties = new ConverterProperties();
FontProvider provider = new FontProvider();
//这里是为了解决中文无法转换的问题,自己将黑体的字体包放到对应目录读取并载入,用的时候可以视情况替换路径(我这边是直接塞到引入的jar包里面,读取的class文件目录)
InputStream stream = ResourceUtil.getResourceStream("net/sf/jasperreports/fonts/dejavu/SIMHEI.TTF");
byte[] fontProgramBytes = StreamUtil.inputStreamToArray(stream);
provider.addFont(fontProgramBytes);
converterProperties.setFontProvider(provider);
HtmlConverter.convertToPdf(html,outputStream,converterProperties);
Long end = Calendar.getInstance().getTimeInMillis();
System.out.println("结束时间--" + end);
System.out.println("耗时 " + (end - start));
}catch (Exception e){
e.printStackTrace();
}
}
}
3、最终打印效果
最后
以上就是懦弱奇迹为你收集整理的JAVA 实现HTML字符串转PDF的全部内容,希望文章能够帮你解决JAVA 实现HTML字符串转PDF所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复