HTML转PDF工具(wkhtmltopdf)介绍,支持widows和linux

来源:互联网 发布:阿里丁丁 mac 编辑:程序博客网 时间:2024/05/18 01:07

最近项目中客户提了一个奇葩的需求;批量把html转为pdf文件用于存档。听到这个需求不知所错,最开始研究iText使用Java开发,各种中文乱码,中文不显示问题。后来在网上搜索到wkhtmltopdf工具,以下是完整的说明以及代码。

首先下载文件:html转为pdf文件(wkhtmltox)(包括windows下exe安装文件和Linux下可执行文件),官方下载地址

一、windows下操作步骤

  1. 安装:wkhtmltox-0.12.3.2_msvc2013-win64.exe,cmd命令进入安装目录

  2. 运行:wkhtmltopdf.exe [参数,可选,可多个;wkhtmltopdf中文参数详解] <需要转的html路径,必填,可多个> <转成功后的pdf文件存放地址,必填>

    a.例子: wkhtmltopdf.exe –page-size A4 www.baidu.com pdf.pdf

二、linux下操作步骤

  1. 进到http://wkhtmltopdf.org/downloads.html页面,下载stable下面的

  2. 解压:命令:tar -xvf wkhtmltox-0.12.3_linux-generic-amd64.tar.xz

  3. 解决中文不显示或乱码问题:需要字体文件cjkuni-uming、smc、stix放入/usr/share/fonts目录下。也可以将 windows下的字体,例如simsun.ttc、msyh.ttf、msyhbd.ttf 复制到 Linux系统  /usr/share/fonts 下

  4. 运行:进入wkhtmltox/bin目录 ./wkhtmltopdf [参数,可选,可多个;wkhtmltopdf中文参数详解] <需要转的html路径,必填,可多个> <转成功后的pdf文件存放地址,必填>

    a.例子: ./wkhtmltopdf –page-size A4 www.baidu.com pdf.pdf

三、通过java调用wkhtmltox的可执行文件实现批量html转pdf

public class HtmlToPdf {    private static final Logger LOG = LoggerFactory.getLogger(HtmlToPdf.class);    private static final String TOPDFTOOL = "/root/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();        // 如果pdf保存路径不存在,则创建路径        if (!parent.exists()) {            parent.mkdirs();        }        StringBuilder cmd = new StringBuilder();        cmd.append(TOPDFTOOL);        cmd.append(" ");        cmd.append("--page-size A2");// 参数        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();            LOG.info("HTML2PDF成功,参数---html路径:{},pdf保存路径 :{}", new Object[] {srcPath, destPath });        } catch (Exception e) {            LOG.error("HTML2PDF失败,srcPath地址:{},错误信息:{}", new Object[]{srcPath, e.getMessage()});            result = false;        }        return result;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
/** * 当java调用wkhtmltopdf时,用于获取wkhtmltopdf返回的内容 */public class HtmlToPdfInterceptor extends Thread {    private static final Logger LOG = LoggerFactory            .getLogger(HtmlToPdfInterceptor.class);    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);            br.readLine();        } catch (IOException e) {            LOG.error(e.getMessage());            e.printStackTrace();        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
/** * 测试 */public class Test {    public static void main(String[] args) {        String htmlPath = "www.baidu.com";        String pdfPath = "/root/pdfFile/testpdf.pdf";        HtmlToPdf.convert(htmlPath, pdfPath );    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

注:html的table中不能用thead,用了后换页会出现两个表头问题如图:
这里写图片描述


转自:http://blog.csdn.net/zhangkezhi_471885889/article/details/52184700

0 0
原创粉丝点击