Java uses wkhtmltox to generate PDF documents or images using HTML code. wkhtmltoxpdf

Source: Internet
Author: User
Tags wkhtmltopdf

Java uses wkhtmltox to generate PDF documents or images using HTML code. wkhtmltoxpdf

Convert HTML code to PDF or image for saving as required by the project. At first, Flying Saucer was used to generate HTML code into PDF documents. The function has been developed and the conversion function can be completed. During this process, Chinese support and image Path Problems were also encountered, it also solved the problem. I thought it would be okay, but during the test, I found that some styles generated by PDF files were problematic. I searched and found that Flying Saucer is an old technology, no one has done it now, so it is unfriendly to support CSS3.0.

If the function is to be implemented and it is unrealistic for others to change the HTML code, try other technologies. After thinking about generating images, I found that using CSSBOX can generate HTML code as an image, and then I tried it, then we are happy to integrate this function into the system. There was no problem all the time, and the tragedy happened again. One page rendered a oblique line using CSS3, and the result CSSBOX could not be converted.

In fact, at the beginning, we knew that wkhtmltopdf could generate HTML code into a PDF document, but this is actually to install a software that calls this software in Java code to execute the conversion, at that time, I thought there was no other technology to directly introduce the jar package, so I didn't try it. Now there is really no way to do it. Just try it. Because I want to generate HTML code as an image, I am using wkhtmltoimage. In fact, there is no difference. When installing wkhtmltox, wkhtmltopdf and wkhtmltoimage will be installed at the same time.

When using wkhtmltoimage, there is no detour, mainly because you are not familiar with Linux and have encountered many problems during installation. Now I will share my self-explored knowledge with you. You can refer to it for your personal opinion. If it is incorrect, I hope you will forgive me. The following describes the detailed usage of calling this soft armor in Java. It can be divided into Windows and Linux environments.

 

1. Software:

Home: http://wkhtmltopdf.org/downloads.html

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

I'm not using the latest version, I'm using version 0.12.2, for: http://download.gna.org/wkhtmltopdf/0.12/0.12.2/

 

2. Installation in Windows

I will not talk about installing software in Windows. After the installation is complete, you can find wkhtmltopdf.exeand wkhtmltoimage.exe in the installation directory. These two execution programs are used to convert PDF files and images. We will discuss how to call them later.

 

3. Installation in Linux

I am in the Workshop.

Download Method, you can directly use the wget http://download.gna.org/wkhtmltopdf/0.12/0.12.2/wkhtmltox-0.12.2_linux-centos6-i386.rpm on Linux to download, if you are using a virtual machine like me, you can also first download to the local machine, then use rz-be in Linux to upload large files. Note that you must add this-be command when using the rz command to upload large files; otherwise, the upload will fail. If you do not have the rz command in Linux, you can use the yum-y install lrzsz command to install the rz and sz commands.

 

Start installation below:

1) Go to the RPM package download directory and execute the rpm-ivh wkhtmltox-0.12.2_linux-centos6-i386.rpm command. The result is as follows:

 

The cause of failure is the lack of xorg-x11-fonts-Type1 and xorg-x11-fonts-75dpi dependencies, so we have to install dependencies first.

2) install the xorg-x11-fonts-Type1 dependency: yum-y install xorg-x11-fonts-Type1.

3) install the xorg-x11-fonts-75dpi dependency: yum-y install xorg-x11-fonts-75dpi.

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

5) after the installation is complete, 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

You do not need to add additional Jar packages when calling Java.

Java call code:

1 public class CustomWKHtmlToPdfUtil {2 3 public String getCommand (String sourceFilePath, String targetFilePath) {4 String system = System. getProperty ("OS. name "); 5 if (system. contains ("Windows") {6 return "D: \ Program Files \ wkhtmltopdf \ wkhtmltoimage.exe" + sourceFilePath + "" + targetFilePath; 7} else if (system. contains ("Linux") {8 return "wkhtmltoimage" + sourceFilePath + "" + targetFilePath; 9} 10 return ""; 11} 12 13 public static void main (String [] args) throws Exception {14 mwmwkhtmltopdfutil util = new CustomWKHtmlToPdfUtil (); 15 String command = util. getCommand ("e:/html/result.html", "e:/html/result.jpg"); 16 Process process = runtime.getruntime(cmd.exe c (command); 17 process. waitFor (); // This call is critical, that is, wait until the current command is executed and then continue to execute 18 System. out. println ("execution completed"); 19} 20}

In fact, it is to use Java to call the command to execute cmd, but to distinguish between Windows and Linux systems, if it is a Windows system, you must specify the location of wkhtmltoimage or wkhtmltopdf, in Windows, the installation path is D: \ Program Files \ wkhtmltopdf. Therefore, the path I used to call wkhtmltoimage is D: \ Program Files \ wkhtmltopdf \ wkhtmltoimage.exe. In a Linux environment, wkhtmltopdf and wkhtmlimage are installed in the system command during software installation. Therefore, you do not need to specify the installation path and call it directly.

We recommend that you use wkhtmltoimage to generate a jpg image. When you call wkhtmltopdf to generate a PDF image, it may be a problem. These are some of my experiences and I hope they will be useful to you!

Related Article

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.