1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > JAVA实现Html转Pdf(wkhtmltopdf)

JAVA实现Html转Pdf(wkhtmltopdf)

时间:2020-12-28 19:26:33

相关推荐

JAVA实现Html转Pdf(wkhtmltopdf)

官网地址: /downloads.html

直接下载对应版本即可

linux 安装命令

yum install xorg-x11-fonts-75dpi.noarchyum install xorg-x11-fonts-Type1.noarchyum install icu.x86_64yum install libjpegyum install libpngyum install libXrenderyum install libXextrpm -ivh /opt/wkhtmltox-0.12.5-1.centos7.x86_64.rpm

linux需要额外安装的插件

yum install xorg-x11-fonts-75dpi.noarchyum install xorg-x11-fonts-Type1.noarchyum install icu.x86_64yum install libjpegyum install libpngyum install libXrenderyum install libXext

linux 默认会安装到 /usr/local/bin/wkhtmltopdf

安装完成后默认还有个 wkhtmltopng的工具

linux 测试命令

wkhtmltopdf ‘html文件地址或者网址’ '生成文件保存地址'

windows 测试命令

cmd 切换到安装目录下wkhtmltopdf.exe ‘html文件地址或者网址’ ‘生成文件保存地址’

不得不说 效果相当的惊艳,样式比某些浏览器的打印功能都好

下面直接上硬货

package com.itender.ms.util;import com.itender.ms.config.WkhtmltopdfConfig;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.ponent;import java.io.File;/*** @author ah* @date /5/14 16:15*/@Componentpublic class HtmlToPdf {private Logger logger = LoggerFactory.getLogger(HtmlToPdf.class);// wkhtmltopdf在系统中的路径private String toPdfTool ="C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe";@Autowiredprivate WkhtmltopdfConfig config;/*** html转pdf** @param srcPath* html路径,可以是硬盘上的路径,也可以是网络路径* @param destPath* pdf保存路径* @return 转换成功返回true*/public boolean convert(String srcPath, String destPath) {File file = new File(destPath);File parent = file.getParentFile();// 如果pdf保存路径不存在,则创建路径if (!parent.exists()) {parent.mkdirs();}StringBuilder cmd = new StringBuilder();if (System.getProperty("os.name").indexOf("Windows") == -1) {cmd.append(config.getPath());//.exe文件路径}else {cmd.append(toPdfTool);}cmd.append(" ");//cmd.append(" \"");cmd.append(srcPath);// cmd.append("\" ");cmd.append(" ");cmd.append(destPath);//logger.info(cmd.toString());boolean result = true;try {Process proc = Runtime.getRuntime().exec(cmd.toString());HtmlToPdfInterceptor error = new HtmlToPdfInterceptor(proc.getErrorStream());HtmlToPdfInterceptor output = new HtmlToPdfInterceptor(proc.getInputStream());error.start();output.start();proc.waitFor();} catch (Exception e) {result = false;e.printStackTrace();}return result;}public static void main(String[] args) {// HtmlToPdf.convert("C:\\Users\\dell\\Desktop\\aaa.html", "D:\\11111111.pdf");}}

package com.itender.ms.util;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;/*** @author ah* @date /5/14 16:25*/public class HtmlToPdfInterceptor extends Thread {private InputStream is;public HtmlToPdfInterceptor(InputStream is){this.is = is;}@Overridepublic void run(){try{InputStreamReader isr = new InputStreamReader(is, "utf-8");BufferedReader br = new BufferedReader(isr);String line = null;while ((line = br.readLine()) != null) {System.out.println(line.toString()); //输出内容}}catch (IOException e){e.printStackTrace();}}}

注意:html文件一定要注明编码,不然会乱码 如

<meta http-equiv=“Content-Type” content=“text/html;charset=utf-8” ></meta>

表格分页问题

需要添加样式 :

能够在最大程度上处理分页时表格问题,但如果一个单元格内容整页都无法放下时同样会出现分页后没有边框问题

/* 样式具体加在哪需要更具实际情况来,有的表格可能是用div而不是table做的 */ttable, tr, td, th, tbody, thead {tpage-break-inside: avoid !important;}

java:

org.jsoup.nodes.Document documentHtml = Jsoup.parse(html);documentHtml.head().append("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" ></meta>");//后台处理乱码documentHtml.head().after("<style type=\"text/css\">\n" +"\ttable, tr, td, th, tbody, thead {\n" +"\tpage-break-inside: avoid !important;\n" +"\n" +"\t}\n" +"\t</style>");//处理表格分页

下面放一些网上找的各大html转pdf工具性能对比图

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