用wkhtmltopdf实现pdf生成与下载

我在这里推荐两种方式
首先需要安装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()
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值