HTML轉PDF工具(wkhtmltopdf)介紹,支援widows和linux

來源:互聯網
上載者:User

最近項目中客戶提了一個奇葩的需求;批量把html轉為pdf檔案用於存檔。聽到這個需求不知所錯,最開始研究iText使用java開發,各種中文亂碼,中文不顯示問題。後來在網上搜尋到wkhtmltopdf工具,以下是完整的說明以及代碼。

首先下載檔案:html轉為pdf檔案(wkhtmltox)(包括windows下exe安裝檔案和linux下可執行檔),官方下載地址

一、windows下操作步驟

安裝:wkhtmltox-0.12.3.2_msvc2013-win64.exe,cmd命令進入安裝目錄

運行:wkhtmltopdf.exe [參數,可選,可多個;wkhtmltopdf中文參數詳解] <需要轉的html路徑,必填,可多個> <轉成功後的pdf檔案存放地址,必填>

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

二、linux下操作步驟

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

解決中文不顯示或亂碼問題:需要字型檔cjkuni-uming、smc、stix放入/usr/share/fonts目錄下

運行:進入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());            HtmlToPdfInter error = new HtmlToPdfInter(                    proc.getErrorStream());            HtmlToPdfInter output = new HtmlToPdfInter(                    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;    }}
/** * 當java調用wkhtmltopdf時,用於擷取wkhtmltopdf返回的內容 */public class HtmlToPdfInter extends Thread {    private static final Logger LOG = LoggerFactory            .getLogger(HtmlToPdfInter.class);    private InputStream is;    public HtmlToPdfInter(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();        }    }}
/** * 測試 */public class Test {    public static void main(String[] args) {        String htmlUrl = "www.baidu.com";        String path = "/root/file/pdf_test.pdf";        HtmlToPdf.convert(htmlUrl , path );    }}

註:html的table中不能用thead,用了後換頁會出現兩個表頭問題如圖:

大概就是這些,如果有什麼問題請留言。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.