环境说明
环境:windows10
客户端软件:wkhtmltopdf
记得配置wkhtmltopdf 全局环境变量,相关的安装步骤可以baidu或者google
wkhtmltopdf 安装包已经上传csdn:
Java 核心功能代码
添加wkhtmltopdf 客户端包装类依赖:
<!--html 转 pdf -->
<dependency>
<groupId>com.github.jhonnymertz</groupId>
<artifactId>java-wkhtmltopdf-wrapper</artifactId>
<version>1.1.11-RELEASE</version>
</dependency>
github 源码地址:https://github.com/jhonnymertz/java-wkhtmltopdf-wrapper
String pdfPath = null;
// 创建pdf 对象
Pdf pdf = new Pdf();
// pdf 数据源(html5 格式字符串)
pdf.addPageFromString(html);
// wkhtmltopdf 添加参数
pdf.addParam(new Param("-O", "Landscape"));
try {
pdfPath = commomPDF();
// 生成pdf 文件保存地址
pdf.saveAs(pdfPath);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
logger.error(e.getMessage());
}
注意:pdf.addPageFromString(html) 这行代码中传递的html 字符串的生成方式=thymeleaf + HTML5页面模板生成。
参考地址:thymeleaf + HTML5页面模板生成html5格式 字符串。
效果展示:
![](https://img-blog.csdnimg.cn/20200311185854547.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3pob3V6aGl3ZW5nYW5n,size_16,color_FFFFFF,t_70)