java如何根据富文本生成pdf文件
发表于:2024-10-28 作者:千家信息网编辑
千家信息网最后更新 2024年10月28日,这篇文章将为大家详细讲解有关java如何根据富文本生成pdf文件,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。示例代码:public class PdfUtil {
千家信息网最后更新 2024年10月28日java如何根据富文本生成pdf文件
这篇文章将为大家详细讲解有关java如何根据富文本生成pdf文件,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
示例代码:
public class PdfUtil { /* * 生成pdf工具类 * wmy 12:40 2019/8/9 * @Param [guideBook, pdfPath] * @return java.lang.Boolean **/ public static Boolean htmlToPdf(GuideBook guideBook, String pdfPath) { try { // 1.新建document Document document = new Document(); // 2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。 //创建 PdfWriter 对象 第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径。 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfPath)); // 3.打开文档 document.open(); //要解析的html //html转换成普通文字,方法如下: org.jsoup.nodes.Document contentDoc = Jsoup.parseBodyFragment(getHtml(guideBook.getTitle())+guideBook.getContent()); org.jsoup.nodes.Document.OutputSettings outputSettings = new org.jsoup.nodes.Document.OutputSettings(); outputSettings.syntax(org.jsoup.nodes.Document.OutputSettings.Syntax.xml); contentDoc.outputSettings(outputSettings); String parsedHtml = contentDoc.outerHtml(); //这儿的font-family不支持汉字,{font-family:仿宋} 是不可以的。 InputStream cssIs = new ByteArrayInputStream("* {font-family: PingFang-SC-Medium.otf;}".getBytes("UTF-8")); //第四个参数是html中的css文件的输入流 //第五个参数是字体提供者,使用系统默认支持的字体时,可以不传。 XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(parsedHtml.getBytes()), cssIs); // 5.关闭文档 document.close(); } catch (Exception e) { e.printStackTrace(); return false; } return true; } /* * 下载文件 * wmy 9:54 2019/8/12 * @Param [request, response, inputStream, fileName] * @return void **/ public static void download(HttpServletRequest request, HttpServletResponse response, InputStream inputStream, String fileName){ BufferedOutputStream bos = null; try { // 定义输出缓冲 10k byte[] buffer = new byte[10240]; //文件名称的处理 // http://127.0.0.1:5002/guide-book/pdf?id=124 fileName = fileName.replaceAll("[\\pP\\p{Punct}]", "-").replace(" ", "-").replaceAll("[-]+", "-")+".pdf"; String userAgent = request.getHeader("user-agent").toLowerCase(); if (userAgent.contains("msie") || userAgent.contains("like gecko")) { fileName = URLEncoder.encode(fileName, "UTF-8"); } else { fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1"); } response.setCharacterEncoding("utf-8"); response.setContentType("application/msword"); response.setHeader("Content-Disposition", "attachment; filename=" + fileName); bos = new BufferedOutputStream(response.getOutputStream()); int bytesRead = 0; while ((bytesRead = inputStream.read(buffer)) != -1) { bos.write(buffer, 0, bytesRead); } } catch (Exception e) { e.printStackTrace(); } finally { if (bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } } /* * 获取html * wmy 10:39 2019/8/12 * @Param [title] * @return java.lang.String **/ public static String getHtml(String title){ return ""+title+"
"; }}
关于"java如何根据富文本生成pdf文件"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
文件
参数
文档
生成
名称
对象
篇文章
UTF-8
富文
书写器
字体
更多
支持
输出
不错
实用
普通
代码
内容
实际
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络安全主题班队会图片
缔邑网络技术
曲靖软件开发学费
深圳软件开发工程师平均工资
软件开发中ie项目是啥意思
数据库和web哪个好用
指定网络安全法
麻将作弊软件开发
陕西回收服务器主板云服务器
linux服务器端口
分享通信网络技术有限公司
绝地求生专门给新手玩的服务器
三级网络技术大题第四题
服务器pcba贴片产线成本
BBS论坛的数据库结构
腾讯网络安全师何忠明
链接代理服务器失败是怎么回事
微信接入自己的服务器
cctv3网络安全
智能社区软件开发功能点估算
财务软件开发人员岗位职责
吱呀服务器链接推荐
华工科技软件开发工资待遇
数据库字段名是status
成都电脑软件开发服务
红帽软件开发
庆阳市国家网络安全宣传周活动
如何超链接其他表格数据库
研发网络安全的公司
数据库表分库分区