Html分两种情况转换为Pdf:

第一种:html的文件
第二钟:html格式的字符串
我们先来讲一下第一种情况:
1.市面上有很多的html转pdf的方法,但是不是受限于中文的限制就是受限于css样式的丢失或者是对html的要求太严格。
所以我在做这个教程的时候找到了一个非常厉害的一个组件首先看一下他的官网:
e-iceblue
他有商业版本和免费的版本,商业版本没购买之前是有水印的,但是可以转换10页,免费版本是没有水印的,但是只支持转换前三页。结合教程使用,我们使用他的免费版本,首先第一步导入他的jar包:

     <dependency><groupId> e-iceblue </groupId><artifactId>spire.doc.free</artifactId><version>3.9.0</version></dependency>

但是中央仓库是没有这个jar包的,所以我们还需要加一个他的jar包仓库地址:

    <repositories><repository><id>com.e-iceblue</id><url>http://repo.e-iceblue.cn/repository/maven-public/</url></repository></repositories>

2.第二步我们使用一下方式读取html文件的内容:

public class HtmlToPDFUtil {public static void main(String[] args) throws IOException{String inputHtml = "C:\\InputHtml.txt";//新建Document对象Document doc = new Document();//添加sectionSection sec = doc.addSection();String htmlText = readTextFromFile(inputHtml);//添加段落并写入HTML文本sec.addParagraph().appendHTML(htmlText);//将文档另存为PDFdoc.saveToFile("C:\\HTMLstringToPDF.pdf", FileFormat.PDF);doc.dispose();}public static String readTextFromFile(String fileName) throws IOException {StringBuffer sb = new StringBuffer();BufferedReader br = new BufferedReader(new FileReader(fileName));String content;while ((content = br.readLine()) != null) {sb.append(content);}return sb.toString();}
}

这个时候就会在c盘目录下生成InputHtml.txt对应的HTMLstringToPDF.pdf文件
第二种方法,html为文本格式的情况:
1.导入上的jar包之后之间将html的文本内容赋值给htmlTest

 public static void main(String[] args) throws IOException{//新建Document对象Document doc = new Document();//添加sectionSection sec = doc.addSection();String htmlText = " <tr>\n" +"        <td colspan=\"8\">\n" +"          <div class=\"yiban\">\n" +"            <span class=\"jiachu\">联系电话:<span>18888888888</span></span>\n" +"          </div>\n" +"          <div class=\"yiban\">\n" +"            <span class=\"jiachu\">送货单号:</span><span>1567894</span>\n" +"          </div>\n" +"        </td>\n" +"      </tr>";//添加段落并写入HTML文本sec.addParagraph().appendHTML(htmlText);//将文档另存为PDFdoc.saveToFile("C:\\HTMLstringToPDF.pdf", FileFormat.PDF);doc.dispose();}

这个情况也是一样的

拓展:将生成的pdf文件返回给前端以供下载

需要一下的代码段,直接贴出来供大家参考:

 public static void downloadPdf(String fileName, String path) {ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletResponse response = requestAttributes.getResponse();response.setContentType("application/force-download");try {response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));} catch (Exception e) {e.printStackTrace();}File file = new File(path);InputStream is = null;ServletOutputStream os = null;try {is = new FileInputStream(file);os = response.getOutputStream();int b;while ((b = is.read()) != -1) {os.write(b);}} catch (FileNotFoundException e) {ExceptionUtils.logError(e);} catch (IOException e) {ExceptionUtils.logError(e);} finally {try {if (null != os) {os.close();}if (null != is) {is.close();}} catch (IOException e) {ExceptionUtils.logError(e);}}}

使用该方法:

HtmlToPDFUtil.downloadPdf(fileName,tmplPath+fileName);

这样就会将pdf文件作为response返回给前端,前端做对应的操作就能将文件下载下来。

Java中将Html转换为PDF相关推荐

  1. java docx转pdf_如何在Java中将DOCX转换为PDF

    自从Microsoft Word 2003中引入DOCX格式以来,由于其易于编辑和深入的设计选择,DOCX格式一直在全球各地的办公室中享有很高的知名度.但是9当涉及到兼容性,尤其是最终用户的查看一致性 ...

  2. doc转pdf java_在java中将.doc转换为.pdf(免费)

    我创建了一个java应用程序,在输出中创建一些文档 . 这些文档是使用apache POI api创建的,由文本abn表组成 . 我的老板现在决定他们也希望用pdf格式存储它们 . 他们当然有0美元的 ...

  3. 在Java中将AI转换为PSD,JPEG或PNG图像格式指南

    Adobe Illustrator文件可用于将构想变为现实.但是,许多应用程序未广泛支持此文件格式.因此,可能需要将AI文件转换为不同的栅格图像和其他文件格式. 为了解决此问题,可以在Java应用程序 ...

  4. 在Java中将boolean转换为int

    本文翻译自:Convert boolean to int in Java 在Java中将boolean转换为int的最常用方法是什么? #1楼 参考:https://stackoom.com/ques ...

  5. 在Java中将Double转换为Integer

    本文翻译自:Cast Double to Integer in Java Any way to cast java.lang.Double to java.lang.Integer ? 有什么方法可以 ...

  6. 如何在Java中将String转换为int?

    如何在Java中将String转换为int ? 我的字符串仅包含数字,我想返回它代表的数字. 例如,给定字符串"1234" ,结果应为数字1234 . #1楼 好吧,要考虑的一个非 ...

  7. 如何在Java中将String转换为int

    在本教程中,我们将看到将Java中的String转换为int(或Integer)的各种方法. 您可以使用以下任何一种方式: –使用Integer.parseInt(string) –使用Integer ...

  8. 在Java中将字符串转换为日期,将日期转换为字符串

    Sometimes we have to Convert String to Date in java program or convert Date to String in a different ...

  9. 在Java中将字符串转换为char数组,将char数组转换为String

    Today we will learn how to convert String to a char array and then char array to String in Java. 今天, ...

最新文章

  1. 自定义的GridView控件源代码
  2. 阿里云云服务器的端口配置问题
  3. java打印6个偶数_Java编写一个应用程序,打印所有偶数从2到100
  4. MFC中绘制高亮的图标 VC图标填充半透明色
  5. python 列表 mysql in_关于mysql:内嵌要在python MySQLDB IN子句中使用的列表
  6. java streams_使用JShell的Java 9 Streams API
  7. [转载] Java中的静态方法不能被子类重写
  8. 关于BeautifulSoup写class和class_
  9. Unity官方宣传片Adam 播放地址
  10. mysql mongodb插件_FLinkX的Mongodb插件优化(三)
  11. go mysql slave_MySQL基于GTIDs的MySQL Replication-Go语言中文社区
  12. Mysql在window下的表现_Mysql在windows系统下的配置
  13. nginx504超时解决方法
  14. 介绍两个Ubuntu上的桌面小工具
  15. 如何修改leaflet的marker图标
  16. 用VC实现洪水***程序
  17. HTML5CSS3网页设计仿微信通讯录页
  18. 蜜雪冰城“土”里刨金
  19. 07-11-Exchange Server 2019-配置-Outlook 2019-IMAP4
  20. 混合策略与混合策略纳什均衡

热门文章

  1. hybris mysql_hybris使用mysql/sqlserver配置
  2. http网址捆綁代理php_决心下载win7原版系统没有捆绑软件没有广告的win7原版系统镜像...
  3. 关于公司的下一步,如何找到革命性的商业点子?
  4. vc++网络安全编程范例(19)实现数字信封打包与拆解
  5. 4月1日反‘愚人节’手册大全
  6. 每个python文件就是一个模块、模块的名字就是_【填空题】每个Python文件都可以作为一个模块,模块的名字就是 的名字。...
  7. 微信小程序不支持打开非业务域名_一行代码网站封装微信小程序,并解决微信小程序不支持打开非业务域名https://,请重新配置的问题!...
  8. 有奖征集丨向100万云开发者“秀肌肉”的机会来了
  9. ES6中的Promise
  10. 简单选择排序显示第K趟