html转换pdf

html2pdf 是itext提供的网页转PDF包

老版本的

itextpdf 对html标签转换支持太差,现在升级到最新版本

1,使用版本maven

1

2

3

4

5

  <dependency>

    <groupId>com.itextpdf</groupId>

    <artifactId>html2pdf</artifactId>

    <version>2.1.5</version>

</dependency>

2, 示例

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

String html="<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/><title>First parse</title></head>" +

               "<body  style=\"font-family: SimSun\"><div class=\"t1 pl2\" style=\"  margin: 0;  display: flex;flex-wrap: wrap;align-items: center;\">\n" +

               "        <div class=\"ml15\" style=\"display: inline-block;\"><input id=\"ck1\" checked=\"checked\" type=\"checkbox\"><label for=\"ck1\">嘟嘟嘟</label></div>\n" +

               "        <div class=\"ml15\" style=\"display: inline-block;\"><input id=\"ck2\" type=\"checkbox\"><label for=\"ck2\">嘿嘿嘿</label></div>\n" +

               "        <div class=\"ml15\" style=\"display: inline-block;\"><input id=\"ck3\" type=\"checkbox\"><label for=\"ck3\">哈哈哈</label></div>\n" +

               "        <div class=\"ml15\" style=\"display: inline-block;\"><input id=\"ck4\" type=\"checkbox\"><label for=\"ck4\">啦啦啦</label></div>\n" +

               "    </div><div><ul>\n" +

               "<li>Coffee</li>\n" +

               "<li>Milk</li>\n" +

               "<li>可以选择</li>\n" +

               "</ul></div></body></html>";

 

       //pdf转换配置类

       ConverterProperties converterProperties=new ConverterProperties();

 

       //中文字体

       String a= "D:\\work\\test\\src\\main\\webapp\\static\\dsPdf\\SONGTI.TTF";

 

       FontProvider fontProvider=new FontProvider();

       fontProvider.addStandardPdfFonts();

       fontProvider.addFont(a);

       converterProperties.setFontProvider(fontProvider);

       converterProperties.setCharset("UTF-8");

       //输出地址

       PdfWriter pdfWriter =new PdfWriter(new FileOutputStream("D:\\work\\test\\src\\main\\webapp\\upload\\2019-10-28"+"/a.pdf"));

       //开始转换

       HtmlConverter.convertToPdf(html,pdfWriter,converterProperties);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值