写这篇文章 主要是整个百度找不到一篇使用openhtmltopdf的文章 没有一个演示项目,github上面只有简单的演示,看不出效果。
openhtmltopdf 是 html片段转pdf文件的一个开源框架 源码在github上,地址是https://github.com/danfickle/openhtmltopdf
目前html片段 不支持js,支持图片、部分css3样式、部分h5样式。下面演示简单用法。
1.pom.xml引入
<dependency> <!-- ALWAYS required. --> <groupId>com.openhtmltopdf</groupId> <artifactId>openhtmltopdf-core</artifactId> <version>${openhtml.version}</version></dependency><dependency> <!-- Required for PDF output. --> <groupId>com.openhtmltopdf</groupId> <artifactId>openhtmltopdf-pdfbox</artifactId> <version>${openhtml.version}</version></dependency><dependency> <!-- Required for image output only. --> <groupId>com.openhtmltopdf</groupId> <artifactId>openhtmltopdf-java2d</artifactId> <version>${openhtml.version}</version></dependency><dependency> <!-- Optional, leave out if you do not need right-to-left or bi-directional text support. --> <groupId>com.openhtmltopdf</groupId> <artifactId>openhtmltopdf-rtl-support</artifactId> <version>${openhtml.version}</version></dependency><dependency> <!-- Optional, leave out if you do not need HTML5 parsing support. --> <groupId>com.openhtmltopdf</groupId> <artifactId>openhtmltopdf-jsoup-dom-converter</artifactId> <version>${openhtml.version}</version></dependency><dependency> <!-- Optional, leave out if you do not need logging via slf4j. --> <groupId>com.openhtmltopdf</groupId> <artifactId>openhtmltopdf-slf4j</artifactId> <version>${openhtml.version}</version></dependency><dependency> <!-- Optional, leave out if you do not need logging via log4j. --> <groupId>com.openhtmltopdf</groupId> <artifactId>openhtmltopdf-log4j</artifactId> <version>${openhtml.version}</version></dependency><dependency> <!-- Optional, leave out if you do not need SVG support. --> <groupId>com.openhtmltopdf</groupId> <artifactId>openhtmltopdf-svg-support</artifactId> <version>${openhtml.version}</version></dependency><dependency> <!-- Optional, leave out if you do not need MathML support. --> <!-- Introduced in RC-13. --> <groupId>com.openhtmltopdf</groupId> <artifactId>openhtmltopdf-mathml-support</artifactId> <version>${openhtml.version}</version></dependency>
2.代码简单使用
public class OpenHtmlToPDF { public static void main(String[] args) { try (OutputStream os = new FileOutputStream("F://pdf//pdfbox//out3.pdf")) { // 输出的pdf PdfRendererBuilder builder = new PdfRendererBuilder(); builder.useFastMode(); //下面这个方法是要自己指定 字体文件 不然转出的pdf文件中 中文会变成#### builder.useFont(new FSSupplier<InputStream>() { @Override public InputStream supply() { try { //指定 字体文件 return new FileInputStream("F://pdf//pdfbox//simhei.ttf"); } catch (FileNotFoundException e) { e.printStackTrace(); } return null; } }, "simhei", 400, BaseRendererBuilder.FontStyle.NORMAL, true); //第二个参数 一定要和文件名一样!!作用在html页面上 builder.withHtmlContent(readTxt("F://pdf//pdfbox//pdf-css3.html"), Surrogate.Generator.class.getResource("/root.htm").toExternalForm()); //第一个参数是html页面 第二个参数 是一个全空的文件 里面什么都没有 但是后缀一定要是。htm 作用类似于一个画板 如果不添加这个参数或者置为null 则html文件中的图片 不会转化 所以一定需要加 builder.toStream(os); builder.run(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }
}
3.html页面注意事项
1.关于字体 。
如果自己指定字体文件,那么要在html页面上 加入
<style>
* {
font-family: 'simhei'; //value是指定字体时 第二个参数
}
</style>
2.标签一定要有闭合 不然会解析失败
效果
转出来的pdf