我在这里推荐两种方式
首先需要安装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();

本文介绍了两种在Java中利用wkhtmltopdf生成PDF的方法。第一种是通过直接调用命令行工具,提供相关代码示例;第二种是使用java-wkhtmltopdf-wrapper库,详细说明了引入依赖和编写代码的过程。
最低0.47元/天 解锁文章
577

被折叠的 条评论
为什么被折叠?



