html2pdf 是itext提供的网页转PDF包
老版本的
itextpdf 对html标签转换支持太差,现在升级到最新版本
1,使用版本maven
<dependency> <groupId>com.itextpdf</groupId> <artifactId>html2pdf</artifactId> <version>2.1.5</version> </dependency>
2, 示例
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;"> " + " <div class="ml15" style="display: inline-block;"><input id="ck1" checked="checked" type="checkbox"><label for="ck1">嘟嘟嘟</label></div> " + " <div class="ml15" style="display: inline-block;"><input id="ck2" type="checkbox"><label for="ck2">嘿嘿嘿</label></div> " + " <div class="ml15" style="display: inline-block;"><input id="ck3" type="checkbox"><label for="ck3">哈哈哈</label></div> " + " <div class="ml15" style="display: inline-block;"><input id="ck4" type="checkbox"><label for="ck4">啦啦啦</label></div> " + " </div><div><ul> " + "<li>Coffee</li> " + "<li>Milk</li> " + "<li>可以选择</li> " + "</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);