Home  >  Article  >  Java  >  Java uses wkhtmltox to implement HTML code to generate PDF documents or pictures

Java uses wkhtmltox to implement HTML code to generate PDF documents or pictures

高洛峰
高洛峰Original
2016-12-02 16:13:452905browse

Due to project needs, convert the HTML code to PDF or image for saving. I first used Flying Saucer to generate HTML codes into PDF documents. The function has been developed and the conversion function can be completed. During this period, I also encountered problems with Chinese support and image paths, which were also solved. I thought this would be enough, but During the test, I found that there were problems with the styles generated by some PDF documents. After searching, I found that Flying Saucer is an older technology and no one is doing it anymore, so it is not very friendly to CSS3.0 support.

The function needs to be implemented, and it is unrealistic to let others change the HTML code, so try other technologies. Later, I thought that it would be useful to generate images, so I searched for it and found that I could use CSSBOX to generate HTML codes into images. Then I tried it, and sure enough, it worked. Then I happily integrated this function into the system. There has been no problem, but tragedy happened again. One page used CSS3 to render a diagonal straight line, but the CSSBOX could not be converted.

In fact, at the beginning, I knew that wkhtmltopdf can generate HTML codes into PDF documents, but this actually requires installing a software and calling this software in Java code to perform the conversion. At that time, I felt that there was no other technology that could directly introduce the jar package. , I haven’t tried it. There is really no other way now, so let’s give it a try. Because I want to generate HTML code into pictures now, I am using wkhtmltoimage. In fact, there is no difference. When installing wkhtmltox software, wkhtmltopdf and wkhtmltoimage will be installed at the same time.

There were many detours when using wkhtmltoimage, mainly because I was not familiar with Linux and encountered many problems during installation. Now I would like to share the knowledge I have gained with you so that you can refer to it. It is just my personal opinion and I hope you will forgive me for any inaccuracies. Next, we will introduce the detailed usage of calling this soft armor in Java. Specifically divided into Windows environment and Linux environment.

1. Software download address:

Official download address homepage: http://wkhtmltopdf.org/downloads.html

You can download the latest version of wkhtmltox on this page.

I am not using the latest version. I am using version 0.12.2. The download address is: http://download.gna.org/wkhtmltopdf/0.12/0.12.2/

2. Windows environment installation

I won’t go into details about installing software in Windows environment. I downloaded the version of wkhtmltox-0.12.2_msvc2013-win64.exe from http://download.gna.org/wkhtmltopdf/0.12/0.12.2/. For 32-bit users, you can choose the 32-bit version to download. After the download is complete, double-click to install. After the installation is complete, you can find wkhtmltopdf.exe and wkhtmltoimage.exe in the installation directory. These two execution programs are what we use to convert PDF and convert images. We will talk about how to call them later.

3. Linux environment installation

I downloaded this version of RPM wkhtmltox-0.12.2_linux-centos6-i386.rpm at the address http://download.gna.org/wkhtmltopdf/0.12/0.12.2/ Package, since my virtual machine is installed with 32-bit version of CentOS 6.5, I downloaded this version. You can download the Linux version and 32-bit and 64-bit version according to the actual situation. You can choose according to the actual situation. That’s it.

Download method, you can use wget http://download.gna.org/wkhtmltopdf/0.12/0.12.2/wkhtmltox-0.12.2_linux-centos6-i386.rpm directly on Linux to download, if you use it like me It is a virtual machine. You can also download it to the local machine first, and then use rz -be to upload it on Linux. Note that when using the rz command to upload large files, you must add the -be command, otherwise the upload will not be successful. If there is no rz command in your Linux, you can use the yum -y install lrzsz command to install the rz and sz commands.

Start the installation below:

1) Enter the directory where the RPM package was downloaded and execute the rpm -ivh wkhtmltox-0.12.2_linux-centos6-i386.rpm command. The result is as follows:

Java uses wkhtmltox to implement HTML code to generate PDF documents or pictures

Failed The reason is that the xorg-x11-fonts-Type1 and xorg-x11-fonts-75dpi dependencies are missing, so we need to install the dependencies first.

2) Install xorg-x11-fonts-Type1 dependencies: yum -y install xorg-x11-fonts-Type1.

3) Install xorg-x11-fonts-75dpi dependencies: yum -y install xorg-x11-fonts-75dpi.

4) Install wkhtmltox again: rpm -ivh wkhtmltox-0.12.2_linux-centos6-i386.rpm, OK, the installation is successful. If you are missing other dependencies during installation, you need to install the missing dependencies first and then install wkhtmltox. The method to query missing dependencies is yum search xxxx, xxxx is the missing dependency keyword, and then find the correct yum source from the query results for installation.

5) After the installation is completed, we can use wkhtmltoimage http://www.baidu.com 1.jpg or wkhtmltopdf http://www.baidu.com 1.pdf to test whether the installation is successful.

4. Java program call

When calling Java, there is no need to add additional Jar packages.

Java calling code:

public class CustomWKHtmlToPdfUtil {

    public String getCommand(String sourceFilePath, String targetFilePath) {
        String system = System.getProperty("os.name");
        if(system.contains("Windows")) {
            return "D:\\Program Files\\wkhtmltopdf\\wkhtmltoimage.exe " + sourceFilePath + " " + targetFilePath;
        }else if(system.contains("Linux")) {
            return "wkhtmltoimage " + sourceFilePath + " " + targetFilePath;
        }
        return "";
    }

    public static void main(String[] args) throws Exception{
        CustomWKHtmlToPdfUtil util = new CustomWKHtmlToPdfUtil();
        String command = util.getCommand("e:/html/result.html", "e:/html/result.jpg");
        Process process = Runtime.getRuntime().exec(command);
        process.waitFor();  //这个调用比较关键,就是等当前命令执行完成后再往下执行
        System.out.println("执行完成");
    }
}

   其实就是使用Java调用cmd的执行命令,但是调用要区分Windows和Linux系统,如果是Windows系统,则必须要指定wkhtmltoimage或者wkhtmltopdf的位置,我在Windows的安装位置是D:\Program Files\wkhtmltopdf,因此我在上面调用wkhtmltoimage时使用的路径是D:\Program Files\wkhtmltopdf\wkhtmltoimage.exe。在Linux环境下,由于软件在安装时,会把wkhtmltopdf和wkhtmlimage安装到系统命令中,因此不需要指定安装路径,直接调用就可以了。

    我比较推荐使用wkhtmltoimage生成JPG图片,我调用wkhtmltopdf生成PDF时会有点问题。以上是我的一些经历,希望对大家有用!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn