java pdf 富文本_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,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+"

";

}

}

测试结果:

6491aaaeddcf2bf15455e1710d679eab.gif

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

相关文章

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值