环境说明
环境: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 源码地址:/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 blocklogger.error(e.getMessage());}
注意:pdf.addPageFromString(html) 这行代码中传递的html 字符串的生成方式=thymeleaf + HTML5页面模板生成。
参考地址:thymeleaf + HTML5页面模板生成html5格式 字符串。