Java implements conversion from Html to PDF, and javahtmlpdf

Source: Internet
Author: User
Tags wkhtmltopdf

Java implements conversion from Html to PDF, and javahtmlpdf

The customer of the Project puts forward a requirement to export form data in the government affairs process to pdf or image format for electronic archival materials. Forms are implemented based on the company's e-government construction platform, and are saved in the database in html format. Therefore, we intend to convert the form html into pdf or image directly. Since the form has already been written into the html page, all I need to do is parse the pdf generation tool of html + css perfectly. Most of the results of searching html to pdf on Baidu use itext, which is indeed the first choice for java open-source components. However, itext also has limitations, that is, to write templates by yourself. There are hundreds of forms in the system. It is unrealistic to create an export template for each form.

Finally, wkhtmltopdf entered my selection range. Wkhtmltopdf is a tool developed using the webkit web rendering engine to convert html into pdf. It can be integrated with Multiple scripting languages to convert documents.

Official Website address http://wkhtmltopdf.org/

Github address https://github.com/wkhtmltopdf/wkhtmltopdf

Wkhtmltopdf converting html into pdf is easy, as long as you enter

C: \ wkhtmltow..exe http://www.csdn.net c: \ csdn.pdf

You can convert the csdn web page into a pdf file and save it to the C-drive root directory.

Call the wkhtmltopdfcommand runtime.getruntime(cmd.exe c ("c: \ wkhtmltopdf.exe http://www.csdn.net c: \ csdn.exe") in Java to implement the conversion.

The following command is encapsulated into a java tool class for convenient calling.

Import java. io. file; public class HtmlToPdf {// path of wkhtmltopdf in the system private static final String tow.tool = "c :\\ wkhtmltow..exe "; /*** convert html to pdf * @ param srcPath html path, which can be the path on the hard disk, it can also be the network path * @ param destPath pdf save path * @ return returns true if the conversion is successful */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 (srcPath); cmd. append (""); cmd. append (destPath); boolean result = true; try {Process proc = runtime.getruntime(cmd.exe c (cmd. toString (); HtmlToPdfInterceptor error = new HtmlToPdfInterceptor (proc. getErrorStream (); htmltow.interceptor output = new htmltow.interceptor (proc. getInputStream (); error. start (); output. start (); proc. waitFor ();} catch (Exception e) {result = false; e. printStackTrace ();} return result ;}}

When receiving Process input and error information, you need to create another thread. Otherwise, the current thread will remain waiting (this occurs in Tomcat ).

Import java. io. bufferedReader; import java. io. IOException; import java. io. inputStream; import java. io. inputStreamReader;/*** when java calls wkhtmltopdf, it is used to obtain the content returned by wkhtmltopdf */public class HtmlToPdfInterceptor extends Thread {private InputStream is; public HtmlToPdfInterceptor (InputStream is) {this. is = is;} public void run () {try {InputStreamReader isr = new InputStreamReader (is, "UTF-8"); BufferedReader br = new BufferedReader (isr); String line = null; while (line = br. readLine ())! = Null) {System. outlprintln (line. toString (); // output content} catch (IOException e) {e. printStackTrace ();}}}

Call in Servlet

/*** Convert Html to PDF */@ WebServlet ("/htmltopdf/servlet") public class htmltow.servlet extends HttpServlet {private static final long serialVersionUID = 1L; /*** Servlet receives the parameter path and obtains the html url */protected void service (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String path = request. getParameter ("path"); if (path = null | path. equals ("") {return ;} // obtain the temporary pdf save path // tmp is the directory under the website // put the generated pdf under the website to download String pdfPath = request. getSession (). getServletContext (). getRealPath ("/tmp"); String parameter name = UUID. randomUUID (). toString () + ". pdf "; if (HtmlToPdf. convert (path, pdfPath + "/" + upload name) {response. sendRedirect (request. getContextPath () + "/tmp/" + consumer name );}}}

In the browser, enter http: // <website path>/htmltopdf/servlet? Path = http://blog.csdn.net


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.