我在这里推荐两种方式
首先需要安装wkhtmltopdf,不同系统有不同的安装反思,同时这也是它的缺点
第一种方式,写纯的命令,我直接贴实现代码,对原理感兴趣的自己去挖掘
输出流工具
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();
}
}
}
pdf生成工具
public class HtmlToPdf {
/**
* wkhtmltopdf在windows系统中的路径
*/
private static final String toPdfTool = "D:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe";
/**
* wkhtmltopdf在linux系统中的路径
*/
//private static final String toPdfTool ="//home1//software//wkhtmltopdf//wkhtmltox//bin//wkhtmltopdf";
/**
* 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()