1.本文介绍java的零碎要点使用之java操作wkhtmltopdf实现Html转PDF;
2.项目中刚开始用的itext把html转换成pdf,但是itext只能转比较规整的html,如果有比如从
editor这种网页编辑的html的话,就有问题了,会报错;
3.所以后来又找了个wkhtmltopdf:
做java开发的都知道,java生成pdf大部分都是用itext,itext的确是java开源组件的第一选择。不过itext也有局限,就是要自己写模版,系统中的表单数量有好几百个,为每个表单做一个导出模版不现实。 所以找了个直接可以调用的工具wkhtmltopdf,将生成好的html直接转换成pdf。功能很强大。
wkhtmltopdf是一个使用webkit网页渲染引擎开发的用来将 html转成 pdf的工具,可以跟多种脚本语言进行集成来转换文档。
立即学习“Java免费学习笔记(深入)”;
wkhtmltopdf把html转成pdf很简单,只要在windows命令行中输入c:\wkhtmltopdf.exe http://www.cnblogs.com c:\cnblogs.pdf
就可以把博客园网页转成pdf,并保存到C盘根目录。
在java中调用wkhtmltopdf的命令Runtime.getRuntime().exec("c:\wkhtmltopdf.exe http://www.cnblogs.com c:\cnblogs.pdf")就可以实现转换。
下面把命令封装成java工具类,方便调用。

/**
* 版权所有(C) 2016 www.xiongge.club
* @author xsw
* @date 2016-12-8 上午10:17:33
*/
package wkhtmltopdf;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* @ClassName: HtmlToPdfInterceptor
* @Description: TODO()
* @author xsw
* @date 2016-12-8 上午10:17:33
*
*/
public class HtmlToPdfInterceptor extends Thread {
private InputStream is;
public HtmlToPdfInterceptor(InputStream is){
this.is = is;
}
public 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();
}
}
}
/**
* 版权所有(C) 2016 www.xiongge.club
* @author xsw
* @date 2016-12-8 上午10:14:54
*/
package wkhtmltopdf;
import java.io.File;
/**
* @ClassName: HtmlToPdf
* @Description: TODO()
* @author xsw
* @date 2016-12-8 上午10:14:54
*
*/
public class HtmlToPdf {
//wkhtmltopdf在系统中的路径
private static final String toPdfTool = "D:\\wkhtmltopdf\\bin\\wkhtmltopdf.exe";
/**
* html转pdf
* @param srcPath html路径,可以是硬盘上的路径,也可以是网络路径
* @param destPath pdf保存路径
* @return 转换成功返回true
*/
public static 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){
//非windows 系统
toPdfTool = FileUtil.convertSystemFilePath("/home/ubuntu/wkhtmltox/bin/wkhtmltopdf");
}
cmd.append(toPdfTool);
cmd.append(" ");
cmd.append(" --header-line");//页眉下面的线
//cmd.append(" --header-center 这里是页眉这里是页眉这里是页眉这里是页眉 ");//页眉中间内容
cmd.append(" --margin-top 3cm ");//设置页面上边距 (default 10mm)
cmd.append(" --header-html file:///"+WebUtil.getServletContext().getRealPath("")+FileUtil.convertSystemFilePath("\\style\\pdf\\head.html"));// (添加一个HTML页眉,后面是网址)
cmd.append(" --header-spacing 5 ");// (设置页眉和内容的距离,默认0)
//cmd.append(" --footer-center (设置在中心位置的页脚内容)");//设置在中心位置的页脚内容
cmd.append(" --footer-html file:///"+WebUtil.getServletContext().getRealPath("")+FileUtil.convertSystemFilePath("\\style\\pdf\\foter.html"));// (添加一个HTML页脚,后面是网址)
cmd.append(" --footer-line");//* 显示一条线在页脚内容上)
cmd.append(" --footer-spacing 5 ");// (设置页脚和内容的距离)
cmd.append(srcPath);
cmd.append(" ");
cmd.append(destPath);
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("http://www.cnblogs.com/xionggeclub/p/6144241.html", "d:/wkhtmltopdf.pdf");
}
}
附上wkhtmltopdf 参数详解

wkhtmltopdf [OPTIONS]... [More input files]
相关文章:
PHP WkHtmlToPdf/WkHtmlToImage 将网页直接转换成pdf和图片
Java使用wkhtmltox实现HTML代码生成PDF文档或者图片
相关视频:











