大量输出报告等场景需要把html导出为pdf,java将html转pdf的方式有很多,但最大名鼎鼎的就是itext,iText是著名的用于生成PDF文档的一个java类库,而其中组件html2pdf是专门用于将html转pdf的。html2pdf提供了类HtmlConverter可以很方便的将html导出为pdf。
官方文档:https://itextpdf.com/en/resources/books/itext-7-converting-html-pdf-pdfhtml/chapter-1-hello-html-pdf
1.pom
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>kernel</artifactId>
<version>7.1.1</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>layout</artifactId>
<version>7.1.1</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>html2pdf</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>
2.java代码
String url="http://www.baidu.com";
Document htmlDocument = Jsoup.connect(url).get();
ConverterProperties converterProperties=new ConverterProperties();
converterProperties.setBaseUri("D:\\pdfTest");
PdfWriter writer = new PdfWriter("d:/pdfTest/test1.pdf");
HtmlConverter.convertToPdf(htmlDocument.html(),writer,converterProperties);
注意:itext不支持svg和javascript,也就是说你的网页包含js脚本才能显示正常,那就不适合用itext来导出,而需要用浏览器渲染后导出.而对于svg的支持itext后面的版本已经优化
However, pdfHTML doesn't support SVG for now, nor does it evaluate JavaScript. Support for SVG is on the development roadmap, and you could solve the JavaScript problem by using a browser engine to preprocess the HTML+CSS+JS. Examples of such a browser engine are WebKit (Chrome, Safari) and Gecko (Firefox). These can interpret the JavaScript and give you HTML that can be rendered by pdfHTML.
NOTE: SVG support was added to pdfHTML 2.1.0 in September 2018, and has been updated and improved in subsequent versions.