JAVA中将html转换成pdf

  前言

  本文是采用了wkhtmltopdf插件的方式进行将html转换成pdf的,首先需要下载该插件,并进行安装(注意区分系统),此处提供windows64的安装包,本文中也是采用此安装包来实现,其他系统的同学可以自行到官网去下载对应系统的安装包,此处不赘述,顺道给出大家下载地址。

windows下载地址:https://pan.baidu.com/s/1eD0NJQymnWgnmPIkBt4Rcg      提取码:dcj7

官网下载地址:https://wkhtmltopdf.org/

  第一步:

下载成功后,安装插件并记录安装的路径

  第二步:

copy我的代码

HtmlToPdf类:
import java.io.File;

public class HtmlToPdf {

    //    wkhtmltopdf在系统中的路径
    private static final String toPdfTool = "D:\soft\wkhtmltopdf\bin\wkhtmltopdf.exe";

    /**
     * html转pdf
     *
     * @param srcPath  html路径,可以是硬盘上的路径,也可以是网络路径
     * @param destPath pdf保存路径
     * @return 转换成功返回true
     */
    public static boolean convert(String srcPath, String destPath,String toPdfTool){
        File file = new File(destPath);
        File parent = file.getParentFile();
        //如果pdf保存路径不存在,则创建路径
        if(!parent.exists()){
            parent.mkdirs();
        }
        StringBuilder cmd = new StringBuilder();
        cmd.append(toPdfTool);
        cmd.append(" ");
        cmd.append(" --header-line");//页眉下面的线
        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) {
        String sourcePath = "https://baike.baidu.com/item/%E5%BC%A0%E4%B8%B9%E5%B3%B0/3740983?fr=aladdin";
        HtmlToPdf.convert(sourcePath, "D:\testpdf.pdf",toPdfTool);
        System.out.println("ojbk");
    }


}
HtmlToPdfInterceptor类:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

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();
        }
    }
}

 至此,大功告成,so easy!!!此插件可以生产本地页面,在线页面,以及页面代码哈。

原文地址:https://www.cnblogs.com/zblwyj/p/13576262.html