前言:工作中,需要将存储的 html 代码转化成pdf,用于数据固化以及方便的在各平台展示,经研究,使用 flying-saucer 来实现这一需求。

构建方式:

  • maven 构建
<!-- FlyingSaucer依赖 https://mvnrepository.com/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:https://github.com/itext/itextpdf

flying-saucer:https://github.com/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 2018年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>办件流水号:2018124442</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" + "                   2018-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;\">经办日期:2018-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>2018-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>办件流水号:2018124442</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" + "                   2018-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;\">经办日期:2018-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>2018-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 2018年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>办件流水号:2018124442</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>本部门已于2018-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;">经办日期:2018-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>2018-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>办件流水号:2018124442</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>本部门已于2018-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;">经办日期:2018-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>2018-01-15 14:34:21</p></div> </div></div></div><p></p>
</body>
</html>

至此,html 转换成 pdf 就实现了。

使用 itext 和 flying-saucer 实现 html 转 PDF相关推荐

  1. flyingsaucer转换多个html,Flying Saucer实现html转pdf(一些有关问题,持续更新)

    Flying Saucer实现html转pdf(一些问题,持续更新) Flying Saucer实现html转pdf(及一些问题,持续更新) 关于FlyingSaucer的使用官网上有详细的介绍. 官 ...

  2. Flying Saucer实现html转pdf(一些问题,持续更新)

    Flying Saucer实现html转pdf(及一些问题,持续更新) 关于FlyingSaucer的使用官网上有详细的介绍. 官网地址: https://today.java.net/pub/a/t ...

  3. iText和flying saucer结合生成pdf的技术

    原博文地址 http://blog.csdn.net/shanliangliuxing/article/details/6833471 下面是我自己利用flying saucer技术生成pdf文档的实 ...

  4. itext转html为pdf 锚点,flying saucer html转pdf经验分享

    对比了很多生成pdf的方案,最终还是决定使用flying saucer,网上能搜出N多相关文章,但是照着做了会出现各种问题,还是得自己一点点摸索,前后花了两天时间才达到自己的目标,下面把过程中的经验总 ...

  5. flying saucer做导PDF踩过的坑~

    最近公司要求做一个导出PDF报表的功能.由于时间比较紧张,而且导出的内容暂时为一个报表而已,所以我采用了flying saucer+freemaker来做. flying sauce源码:https: ...

  6. Flying Saucer 不支持中文,换行,粗体,CheckBox多选框的解决方案

    最近要生成打印版的保单信息,内容比较多,也比较复杂,iText直接生成的话,想必花很多时间,而且可能也很难维护,偶然看到了HTML 在 Fly Saucer的帮助下能转换成PDF,解析CSS还不错,顿 ...

  7. 使用flying saucer将html文件转成PDF

    使用flying saucer生成PDF文件之前,先要使用freemarker生成html文件,然后将html文件转成pdf.生成html文件的东东网上一搜一大堆,大家找找就成. 当然"使用 ...

  8. 使用Flying Saucer生成pdf

    使用Flying Saucer生成pdf 实现思路: 利用FreeMarker编写HTML模板,通过Flying Saucer生成pdf. 一.maven添加依赖 本项目使用maven构建,基于spr ...

  9. iTextRenderer(Flying Saucer) HTML转PDF

    iTextRenderer(Flying Saucer) HTML转PDF iTextRenderer 在依赖 iText 的基础上,单独实现了HTML渲染PDF,基本上能实现 CSS 2.1的整体性 ...

  10. 【CodeForces - 227C】Flying Saucer Segments (思维)

    题干: An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its r ...

最新文章

  1. .NET中做多语言版本的心得 1
  2. 汇编小程序---计算十以内两个数的相加
  3. 云炬Android开发笔记 3-2字体图标库集成与封装
  4. java 信使服务_java – 自行车信使/ TSPPD与OptaPlanner
  5. 多维数据库介绍【转】
  6. 奥运奖牌计数(信息学奥赛一本通-T1064)
  7. Bytom国密网说明和指南
  8. python eval函数_Python基础元素语法总结
  9. HDU 4597 Play Game
  10. Python3.6读写串口操作
  11. 软测基础相关知识回顾
  12. 事故记录-过多进程致使CPU卡死
  13. wordpress最佳架构_生活教练的22个最佳WordPress主题
  14. 如何打开电脑端口——以开启mysql数据库3306端口为例
  15. 什么是图形加速卡(二)
  16. N沟道和P沟道MOS管的四个不同点
  17. Android开发:一个TextView中设置文字不同字体大小和颜色的2种高效方法
  18. SSH协议原理和实践
  19. java在线答题系统,上岸蚂蚁金服!
  20. Html监听Fbx文件加载,Three.js 导入fbx

热门文章

  1. P2837 [USACO08FEB]Dining Cows B 题解
  2. Android 上面实现红外解析(NEC编码)
  3. 关于球型Shader的写法
  4. 读书百客:《嘲鲁儒》赏析
  5. 计算机弹出虚拟U盘,怎么设置vmware虚拟机U盘启动
  6. java 日期 纳秒_java8 ZonedDateTime 日期精度到纳秒
  7. 35个非常出彩的 Flash 网站作品欣赏
  8. 解决win10只有IE可以上网,其他浏览器都无法连接网络
  9. Qt笔记11:qt如何设置应用程序图标和可执行程序图标
  10. BUUCTF 藏藏藏