wkhtmltopdf 实现html转pdf (thead)分页问题

版本:wkhtmltox-0.12.1_msvc2013-win64

解决方式:

就是在需要换页的css属性里面加   

page-break-inside: avoid !important;
代码1:
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();
        }
    }
}
代码2:
public class HtmlToPdf {
    //wkhtmltopdf在系统中的路径
    // private static final String toPdfTool = "D:\\java\\wkhtmltopdf\\bin\\wkhtmltopdf.exe";
    private static final String toPdfTool = "D:\\pdf_tool\\wkhtmltopdf\\bin\\wkhtmltopdf.exe";
    private static final String toPdfToolLinux = "/home/opt/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.getParentFile().mkdirs();
        //如果pdf保存路径不存在,则创建路径

        StringBuilder cmd = new StringBuilder();
        String system = System.getProperty("os.name");
        System.out.println(system);
        boolean contains = system.contains("Windows");
        System.out.println(contains);
        boolean contains2 = system.contains("Linux");
        if(contains){
            cmd.append(toPdfTool);
        }else if(contains2){
            cmd.append(toPdfToolLinux);
        }
        cmd.append(" ");
        cmd.append("  --header-line ");//页眉下面的线
        cmd.append(" --margin-right 20 ");
        cmd.append(" --header-font-size 26 ");
        cmd.append("  --header-right ");//页眉中间内容
        String headerRight="Leyard集团";
        cmd.append(headerRight);

        //cmd.append("  --margin-top 30mm ");//设置页面上边距 (default 10mm)
       // cmd.append(" --header-spacing 30 ");//    (设置页眉和内容的距离,默认0)
        cmd.append(" --footer-line  ");//* 显示一条线在页脚内容上)
        cmd.append(" --footer-left [page]/[topage]  ");
        //cmd.append( "--footer-right [page] ");//设置在中心位置的页脚内容
       // cmd.append(" --footer-spacing 30 ");// (设置页脚和内容的距离)
        cmd.append("  ");
        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://115.28.226.97:5001/create/toLedPage?id=4", "d:/wkhtmltopdf.pdf");
        HtmlToPdf.convert("http://127.0.0.1:8080/create/toLedPage?id=515", "D:/wkhtmltopdf7.pdf");
    }
}


你可能感兴趣的:(文件处理)