700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > wkhtmltopdf 实现html 文档对象转换为pdf 文件核心功能代码

wkhtmltopdf 实现html 文档对象转换为pdf 文件核心功能代码

时间:2023-03-08 18:10:53

相关推荐

wkhtmltopdf 实现html 文档对象转换为pdf 文件核心功能代码

环境说明

环境: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格式 字符串。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。