java html模板转pdf_Java spring boot根据html模板导出PDF

maven依赖

com.itextpdf

itextpdf

5.5.13

com.itextpdf

itext-asian

5.2.0

com.itextpdf

html2pdf

2.1.5

itext官网地址(里面有最新的导出api文档):https://itextpdf.com/en/products/itext-7/pdfhtml

后端controller方法(后端生成对应的pdf文件流返回给前端):

@GetMapping

public void export() throws IOException {

FileReader fileReader1 = new FileReader("XXX/front.html");//  html模板文件body以上部分

String frontHtml = fileReader1.readString();

FileReader fileReader2 = new FileReader("XXX/body.html");//  html模板文件body中的内容

String bodyHtml = fileReader2.readString();

FileReader fileReader3 = new FileReader("XXX/end.html");//   html模板文件body标签后面的内容(可以不用这个,直接用一个string拼接到最后即可)

String endHtml = fileReader3.readString();

OutputStream out = response.getOutputStream();

String a = frontHtml + bodyHtml + endHtml;

ConverterProperties properties = new ConverterProperties();

HtmlConverter.convertToPdf(a, out, properties);

out.flush();

out.close();

}

注:如果只用导出一个html数据,直接读取一次就好,这里是因为需要多个人的动态数据绑定到一个模板中,所以循环body中的内容可以生成多张pdf(因为实际业务需要将多个人的数据使用该模板一次打印多页pdf)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值