HTML to PDF tool (Wkhtmltopdf) introduction, support for widows and Linux

Source: Internet
Author: User
Tags getmessage wkhtmltopdf

Recent projects in the customer to mention a wonderful demand; Batch HTML into PDF file for archiving. Hear this demand do not know the wrong, the first study itext use Java development, a variety of Chinese garbled, Chinese do not show problems. Later on the web to search for the Wkhtmltopdf tool, the following is the complete description and code.

First download files: HTML to PDF file (Wkhtmltox) (including Windows EXE installer file and Linux executable), official download address

First, Windows operation steps

Installation: Wkhtmltox-0.12.3.2_msvc2013-win64.exe,cmd command into the installation directory

Run: wkhtmltopdf.exe [parameters, optional, can be multiple; wkhtmltopdf Chinese parameters] < need to turn the HTML path, required, can be multiple > < successful PDF file storage address, must fill >

A. Example: Wkhtmltopdf.exe–page-size A4 www.baidu.com test.pdf

second, the operation of Linux under the steps

Decompression: command: TAR-XVF wkhtmltox-0.12.3_linux-generic-amd64.tar.xz

Solve the Chinese do not display or garbled problem: Need font file cjkuni-uming, SMC, Stix into the/usr/share/fonts directory

Run: Enter the Wkhtmltox/bin directory./wkhtmltopdf [parameters, optional, can be multiple; wkhtmltopdf Chinese parameter details] < need to turn the HTML path, required, can be multiple > < successful PDF file storage address, must fill >

A. Example:./wkhtmltopdf–page-size A4 www.baidu.com pdf.pdf

third, implement batch HTML to PDF

by invoking Wkhtmltox executable file in Java

public class Htmltopdf {private static final Logger LOG = Loggerfactory.getlogger (Htmltopdf.class);

    private static final String Topdftool = "/root/wkhtmltox/bin/wkhtmltopdf"; /** * HTML to PDF * @param srcpath HTML path, can be the path on the hard disk, can also be network path * @param destpath PDF Save path * @return Conversion successfully returned TR
        UE */public static Boolean convert (String Srcpath, String destpath) {File File = new file (destpath);
        File parent = File.getparentfile ();
        If the PDF save path does not exist, create the path if (!parent.exists ()) {parent.mkdirs ();
        } StringBuilder cmd = new StringBuilder ();
        Cmd.append (Topdftool);
        Cmd.append ("");
        Cmd.append ("--page-size A2");//Parameter 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 succeeded, Parameter---HTML path: {},pdf save path: {}", new object[] {srcpath, destpath});
            catch (Exception e) {log.error ("Html2pdf failed, Srcpath address: {}, error message: {}", new Object[]{srcpath, E.getmessage ()});
        result = false;
    return result; }
}
/**
 * When Java calls Wkhtmltopdf, to get the content returned by wkhtmltopdf/public
class Htmltopdfinter extends Thread {

    private static final Logger LOG = Loggerfactory
            . GetLogger (htmltopdfinter.class);

    Private InputStream is;

    Public Htmltopdfinter (InputStream are) {
        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 ();}}
/**
 * Test
/public class Test {public

    static void Main (string[] args) {
        String htmlurl = "www.ba Idu.com ";
        String Path = "/root/file/pdf_test.pdf";
        Htmltopdf.convert (htmlurl, path);
    }
}

Note: HTML table can not be used THEAD, after the use of the page will appear two table head problem as shown:

That's probably it, please leave a message if you have any questions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.