java uses wkhtmltopdf to convert html into pdf

Posted by Archbob on Sat, 05 Mar 2022 05:05:51 +0100

Install wkhtmltopdf

wkhtmltopdf is an application similar to plug-in, which is supported by windows and linux, but the corresponding program should be installed.

windows Download

Download wkhtmltopdf directly from the official website. Official link: https://wkhtmltopdf.org/downloads.html

Windows installation

1. Download the corresponding windows version
2. Unzip the downloaded software package to your own directory
3. Configure the system environment variable to D:\Program Files\wkhtmltopdf\bin \. This is the installation directory of wkhtmltopdf and the application is here. [or not]
4. Test
On the command line, enter:

wkhtmltopdf http://www.baidu.com/ D:website1.pdf

If you can generate pdf, you will succeed,
You need to use full path if you do not configure environment variables

D:\Program Files\wkhtmltopdf\bin\wkhtmltopdf http://www.baidu.com/ D:website1.pdf

linux Installation

The installation of linux is different from that of windows. You need to download the corresponding version according to the system kernel
1. View the system version
You can use uname -a to view the version

[root@centos7 ~]# uname -a
Linux centos7.kjun 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

You can see that the system is centos7 x86_64, download the installation package of the corresponding version

2. Installation
After downloading the above, get an installation package of RPM. Put it in the system directory and use rpm -ivh wkhtmltox-1:0.12.6-1 centos7. x86_ 64.rpm for installation
Note: during installation, you may encounter lack of dependency and installation failure, as follows:

Everyone's lack of dependence is not necessarily the same. Then, reinstall the installation package of wkhtmltopdf after installing all dependencies using yum.
Just in case: you can install the following dependencies again
  yum -y install libXrender*
  yum -y install libfontconfig*
  yum -y install libXext
  yum -y install libXext.i686
3. Configure environment variables
Add in / etc/profile file

#Modify wkhtmltopdf running environment
export WKHTMLTOPDF_HOME="/usr/local/bin/wkhtmltopdf"
export PATH="$PATH:$WKHTMLTOPDF_HOME/bin"

rpm is usually installed in / usr/local/bin/wkhtmltopdf. If it is not installed, it can be used

whereis wkhtmltopdf

Find out where it is.
4. Test
Same as windows

wkhtmltopdf http://www.baidu.com/ website1.pdf
 perhaps
/usr/local/bin/wkhtmltopdf http://www.baidu.com/ website1.pdf
 No environment variables are configured. Use the second

5. Chinese garbled code
Download the font library and unzip it to the Linux system directory. / usr/share/fonts
Link: Download link

Using java to call wkhtmltopdf

/**
     * @Author ***
     * @Description Convert web pages to pdf 
     * @Date 2022/3/4 14:47
     * @Param [toolPath Software location, srcPath resource location (web address), destPath pdf output path, cookie]
     * @return boolean
     **/
    public static boolean convert(String toolPath, String srcPath, String destPath, Map<String, String> cookie) {
        File file = new File(destPath);
        File parent = file.getParentFile();
        //If the pdf save path does not exist, the path is created
        if (!parent.exists())
            parent.mkdirs();
        StringBuilder cmd = new StringBuilder();
        if (toolPath == null) {
            return false;
        }
        cmd.append(toolPath);
        cmd.append(" ");
        //Set cookie name and value attributes
        //cmd.append("  --cookie ");
        for (String s : cookie.keySet()) {
            cmd.append("  --cookie " + s + " " + cookie.get(s) + "");
        }
//        cmd.append("  --header-line");// Line under header
//        cmd. Append ("-- header center, here is the header, here is the header, here is the header, here is the header")// Middle content of header
        //cmd.append("  --margin-top 30mm ");// Set the top margin of the page (default 10mm)
//        cmd.append(" --header-spacing 10 ");//     (set the distance between the header and the content, 0 by default)
        cmd.append(" --margin-top 10 ");//    (set the distance between the header and the content, 0 by default)
        cmd.append(" --margin-left 20 ");//    (set the distance between the header and the content, 0 by default)
        cmd.append(" --margin-right 20 ");//    (set the distance between the header and the content, 0 by default)
        cmd.append(" --margin-bottom 10 ");//    (set the distance between the header and the content, 0 by default)
//        cmd.append(" --orientation Landscape ");//     (set the distance between the header and the content, 0 by default)
        cmd.append(" --page-size A4 ");//    (set the distance between the header and the content, 0 by default)
//        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;
    }

When called:

convert("/usr/local/bin/wkhtmltopdf", "http://www.baidu.com/website1.pdf", "/test/baidu.pdf",cookies);

In this way, you can bring cookie s and convert the web pages you need to log in to pdf.

be careful

Note that the file name of WKH pdf cannot contain spaces, and the file name of WTM pdf cannot be used

Topics: Java Linux