前言:工作中,需要将存储的 html 代码转化成pdf,用于数据固化以及方便的在各平台展示,经研究,使用 flying-saucer 来实现这一需求。
构建方式:
maven 构建<!-- FlyingSaucer依赖 /artifact/org.xhtmlrenderer/flying-saucer-pdf --><dependency><groupId>org.xhtmlrenderer</groupId><artifactId>flying-saucer-pdf</artifactId><version>9.0.1</version></dependency>
jar 包构建
访问以下链接获取相关jar包,所需 jar 为 iText-2.1.7.jar,flying-saucer-core-9.0.1.jar 和 flying-saucer-pdf.jar
itext:/itext/itextpdf
flying-saucer:/flyingsaucerproject/flyingsaucer
代码实现
package com.xymy.test;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import org.xhtmlrenderer.pdf.ITextFontResolver;import org.xhtmlrenderer.pdf.ITextRenderer;import com.lowagie.text.DocumentException;import com.lowagie.text.pdf.BaseFont;public class HtmlToPdf {public static void main(String[] args) throws Exception {htmlFileToPDF();htmlStringToPdf();}/*** htmlStringToPdf:(html字符串转换成pdf). <br/> * @author xymy* @date 11月8日下午2:59:07* @throws Exception*/private static void htmlStringToPdf() throws Exception {ITextRenderer renderer = new ITextRenderer(); ITextFontResolver fontResolver = renderer.getFontResolver(); fontResolver.addFont("C:/Windows/fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);String outFile = "C:/Users/xymy/Desktop/test2.pdf";OutputStream os = new FileOutputStream(outFile);String htmlstr = "<body style=\"font-family: SimSun\">" + " <p> </p><div><div style=\"font-family: C39HrP60DhTt;font-size: 60px;POSITION: absolute; LEFT: 500px; TOP: 80px;display:none;\"></div><div>\n" + " <div class=\"box_border\" style=\"border: thin solid rgb(0, 0, 0);\">\n" + " <div class=\"title\" style=\"width: 95%; margin-bottom: 20px;\"><p style=\"width: 100%; text-align: center; font-size: 16px; color: rgb(0, 0, 0);\">\n" + " \n" + " \n" + "\n" + " \n" + " 不予行政许可通知书(客户联) \n" + "\n" + " </p></div><div class=\"top\" style=\"border-bottom: 1px dashed rgb(51, 51, 51); min-height: 150px; padding-bottom: 3px;\"><p>负责部门名称:测试单位-审批局 \n" + "</p><p>办件流水号:124442</p>\n" + "\n" + "<p>\n" + " \n" + "</p>\n" + " \n" + " \n" + "<p>\n" + "项目名称:测试项目zwd003 </p>\n" + " \n" + " \n" + " <p>\n" + "\n" + " \n" + " \n" + " \n" + " 企业名称:\n" + " \n" + " \n" + " 世界500强\n" + "</p><p>\n" + " \n" + " \n" + " \n" + " \n" + " 企业地址:\n" + " \n" + " \n" + " 广州\n" + "</p><p>客户联系方式:13751327669<span class=\"span_2\" style=\"margin-left: 120px;\">客户联系人:测试</span></p><p>\n" + "代理人:</p></div><div class=\"content\" style=\"margin-top: 15px; border-bottom: 1px dashed rgb(51, 51, 51); padding-bottom: 2px; min-height: 350px;\"><p>审批事项名称:新企业事项-ylp</p><p>\n" + " <strong>本部门已于\n" + " -01-15 14:34:16\n" + " 将此件办理完毕。</strong>\n" + " </p><p>\n" + " 批准结果:\n" + "\n" + " \n" + " 不批准 \n" + " \n" + "</p><p>\n" + " \n" + " \n" + " </p>\n" + "\n" + "\n" + "\n" + " <p class=\"content_p3\">\n" + " 不批准理由: </p>\n" + "<p class=\"content_p4\">\n" + " 可以办理</p>\n" + "\n" + " \n" + "\n" + " <p class=\"content_p6\">\n" + " <strong>(若对本决定不服,可以直接到本决定之日起60日内,依法申请行政复议,也可以在三个月内依法向人民法院提出行政诉讼。)</strong>\n" + " </p>\n" + "\n" + " </div><div class=\"footer\" style=\"margin-top: 15px; width: 100%; padding-bottom: 25px; bottom: 0px;\"> <p>部门窗口负责人:审批局单位管理员<span class=\"footer_span4\" style=\"margin-left: 182px;\">经办日期:-01-15 14:25:51</span></p><p>经办人:窗口测试001<span class=\"footer_span3\" style=\"margin-left: 150px; display: inline-block;\">监督电话:020-66666666</span></p><p>部门窗口联系电话:020-22222222</p><p style=\"margin-left:410px;margin-top:40px\">(部门行政审批专用章盖印)</p><p>打印人:窗口测试001<span class=\"footer_span3\" style=\"margin-left: 150px; display: inline-block;\">打印日期:</span>-01-15 14:34:21</p></div> \n" + " </div></div></div><div style=\"page-break-before:always\"><br/></div><div><div style=\"font-family: C39HrP60DhTt;font-size: 60px;POSITION: absolute; LEFT: 500px;margin-top:50px;z-index:-1;display:none;\"></div><div>\n" + " <div class=\"box_border\" style=\"border: thin solid rgb(0, 0, 0);\">\n" + " <div class=\"title\" style=\"width: 95%; margin-bottom: 20px;\"><p style=\"width: 100%; text-align: center; font-size: 16px; color: rgb(0, 0, 0);\">\n" + " \n" + " \n" + "\n" + " \n" + " 不予行政许可通知书(存根联) \n" + "\n" + " </p></div><div class=\"top\" style=\"border-bottom: 1px dashed rgb(51, 51, 51); min-height: 150px; padding-bottom: 3px;\"><p>负责部门名称:测试单位-审批局 \n" + "</p><p>办件流水号:124442</p>\n" + "\n" + "<p>\n" + " \n" + "</p>\n" + " \n" + " \n" + "<p>\n" + "项目名称:测试项目zwd003 </p>\n" + " \n" + " \n" + " <p>\n" + "\n" + " \n" + " \n" + " \n" + " 企业名称:\n" + " \n" + " \n" + " 世界500强\n" + "</p><p>\n" + " \n" + " \n" + " \n" + " \n" + " 企业地址:\n" + " \n" + " \n" + " 广州\n" + "</p><p>客户联系方式:13751327669<span class=\"span_2\" style=\"margin-left: 120px;\">客户联系人:测试</span> </p><p>\n" + "代理人:</p></div><div class=\"content\" style=\"margin-top: 15px; border-bottom: 1px dashed rgb(51, 51, 51); padding-bottom: 2px; min-height: 350px;\"><p>审批事项名称:新企业事项-ylp</p><p>\n" + " <strong>本部门已于\n" + " -01-15 14:34:16\n" + " 将此件办理完毕。</strong>\n" + " </p><p>\n" + " 批准结果:\n" + "\n" + " \n" + " 不批准 \n" + " \n" + "</p><p>\n" + " \n" + " \n" + " </p>\n" + "\n" + "\n" + "\n" + " <p class=\"content_p3\">\n" + " 不批准理由: </p>\n" + "<p class=\"content_p4\">\n" + " 可以办理</p>\n" + "\n" + " \n" + "\n" + " <p class=\"content_p6\">\n" + " <strong>(若对本决定不服,可以直接到本决定之日起60日内,依法申请行政复议,也可以在三个月内依法向人民法院提出行政诉讼。)</strong>\n" + " </p>\n" + "\n" + " </div><div class=\"footer\" style=\"margin-top: 15px; width: 100%; padding-bottom: 25px; bottom: 0px;\"> <p>部门窗口负责人:审批局单位管理员<span class=\"footer_span4\" style=\"margin-left: 182px;\">经办日期:-01-15 14:25:51</span></p><p>经办人:窗口测试001<span class=\"footer_span3\" style=\"margin-left: 150px; display: inline-block;\">监督电话:020-66666666</span></p><p>部门窗口联系电话:020-22222222</p><p style=\"margin-left:410px;margin-top:40px\">(客户签署姓名及日期)</p><p>打印人:窗口测试001<span class=\"footer_span3\" style=\"margin-left: 150px; display: inline-block;\">打印日期:</span>-01-15 14:34:21</p></div> \n" + " </div></div></div><p>\n" + "\n" + " </p>" + "</body>";renderer.setDocumentFromString(htmlstr);renderer.layout(); renderer.createPDF(os);os.close();}/*** htmlFileToPDF:(html文件转换成pdf). <br/> * @author xymy* @date 11月8日下午2:59:42* @throws Exception*/private static void htmlFileToPDF() throws Exception {String inputFile = "C:/Users/xymy/Desktop/test.html";String outFile = "C:/Users/xymy/Desktop/test1.pdf";OutputStream os = new FileOutputStream(outFile);ITextRenderer renderer = new ITextRenderer();ITextFontResolver fontResolver = renderer.getFontResolver();fontResolver.addFont("C:/Windows/fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);String url = new File(inputFile).toURI().toURL().toString();renderer.setDocument(url);renderer.layout();renderer.createPDF(os);os.close();}}
附:test.html
<!DOCTYPE html><html><head lang="en"><title>Spring Boot Demo - PDF</title><style>@page {size: 210mm 297mm; /*设置纸张大小:A4(210mm 297mm)、A3(297mm 420mm) 横向则反过来*/margin: 0.25in;padding: 1em;@bottom-center{content:"";font-family: SimSun;font-size: 12px;color:red;};@top-center { content: element(header) };}</style></head><body style="font-family: SimSun"><p></p><div><div style="font-family: C39HrP60DhTt;font-size: 60px;POSITION: absolute; LEFT: 500px; TOP: 80px;display:none;"></div><div><div class="box_border" style="border: thin solid rgb(0, 0, 0);"><div class="title" style="width: 95%; margin-bottom: 20px;"><p style="width: 100%; text-align: center; font-size: 16px; color: rgb(0, 0, 0);">不予行政许可通知书(客户联)</p></div><div class="top" style="border-bottom: 1px dashed rgb(51, 51, 51); min-height: 150px; padding-bottom: 3px;"><p>负责部门名称:测试单位-审批局</p><p>办件流水号:124442</p><p></p><p>项目名称:测试项目zwd003</p><p>企业名称:世界500强</p><p>企业地址:广州</p><p>客户联系方式:13751327669<span class="span_2" style="margin-left: 120px;">客户联系人:测试</span></p><p>代理人:</p></div><div class="content" style="margin-top: 15px; border-bottom: 1px dashed rgb(51, 51, 51); padding-bottom: 2px; min-height: 350px;"><p>审批事项名称:新企业事项-ylp</p><p><strong>本部门已于-01-15 14:34:16将此件办理完毕。</strong></p><p>批准结果:不批准</p><p></p><p class="content_p3">不批准理由:</p><p class="content_p4">可以办理</p><p class="content_p6"><strong>(若对本决定不服,可以直接到本决定之日起60日内,依法申请行政复议,也可以在三个月内依法向人民法院提出行政诉讼。)</strong></p></div><div class="footer" style="margin-top: 15px; width: 100%; padding-bottom: 25px; bottom: 0px;"><p>部门窗口负责人:审批局单位管理员<span class="footer_span4" style="margin-left: 182px;">经办日期:-01-15 14:25:51</span></p><p>经办人:窗口测试001<span class="footer_span3" style="margin-left: 150px; display: inline-block;">监督电话:020-66666666</span></p><p>部门窗口联系电话:020-22222222</p><p style="margin-left:410px;margin-top:40px">(部门行政审批专用章盖印)</p><p>打印人:窗口测试001<span class="footer_span3" style="margin-left: 150px; display: inline-block;">打印日期:</span>-01-15 14:34:21</p></div> </div></div></div><div style="page-break-before:always"><br/></div><div><div style="font-family: C39HrP60DhTt;font-size: 60px;POSITION: absolute; LEFT: 500px;margin-top:50px;z-index:-1;display:none;"></div><div><div class="box_border" style="border: thin solid rgb(0, 0, 0);"><div class="title" style="width: 95%; margin-bottom: 20px;"><p style="width: 100%; text-align: center; font-size: 16px; color: rgb(0, 0, 0);">不予行政许可通知书(存根联)</p></div><div class="top" style="border-bottom: 1px dashed rgb(51, 51, 51); min-height: 150px; padding-bottom: 3px;"><p>负责部门名称:测试单位-审批局</p><p>办件流水号:124442</p><p></p><p>项目名称:测试项目zwd003</p><p>企业名称:世界500强</p><p>企业地址:广州</p><p>客户联系方式:13751327669<span class="span_2" style="margin-left: 120px;">客户联系人:测试</span> </p><p>代理人:</p></div><div class="content" style="margin-top: 15px; border-bottom: 1px dashed rgb(51, 51, 51); padding-bottom: 2px; min-height: 350px;"><p>审批事项名称:新企业事项-ylp</p><p><strong>本部门已于-01-15 14:34:16将此件办理完毕。</strong></p><p>批准结果:不批准</p><p></p><p class="content_p3">不批准理由:</p><p class="content_p4">可以办理</p><p class="content_p6"><strong>(若对本决定不服,可以直接到本决定之日起60日内,依法申请行政复议,也可以在三个月内依法向人民法院提出行政诉讼。)</strong></p></div><div class="footer" style="margin-top: 15px; width: 100%; padding-bottom: 25px; bottom: 0px;"><p>部门窗口负责人:审批局单位管理员<span class="footer_span4" style="margin-left: 182px;">经办日期:-01-15 14:25:51</span></p><p>经办人:窗口测试001<span class="footer_span3" style="margin-left: 150px; display: inline-block;">监督电话:020-66666666</span></p><p>部门窗口联系电话:020-22222222</p><p style="margin-left:410px;margin-top:40px">(客户签署姓名及日期)</p><p>打印人:窗口测试001<span class="footer_span3" style="margin-left: 150px; display: inline-block;">打印日期:</span>-01-15 14:34:21</p></div> </div></div></div><p></p></body></html>
至此,html 转换成 pdf 就实现了。