The customer on the project presents a requirement to export form data from the government process to PDF or image format for use as an electronic archival material. The form is based on the company's e-government building platform, and the database is saved in HTML format, so you plan to convert the form HTML directly into a PDF or a picture. Since the form has already written the HTML page, all I have to do is to parse the HTML+CSS PDF generation tool perfectly. Baidu search for HTML to PDF results, most of the Itext,itext is really a Java open source components of the first choice. But Itext also has limitations, is to write their own template, the number of forms in the system has hundreds of, for each form to do an export template is not realistic.
In the end, wkhtmltopdf into my chosen range. Wkhtmltopdf is a tool developed using the WebKit Web rendering engine to convert HTML into PDF, which can be integrated with a variety of scripting languages to transform documents.
Official Address http://wkhtmltopdf.org/
GitHub Address Https://github.com/wkhtmltopdf/wkhtmltopdf
Wkhtmltopdf to convert HTML to PDF is as simple as typing in the Windows command line
C:\wkhtmltopdf.exe http://www.csdn.net C:\csdn.pdf
You can convert the CSDN Web page to PDF and save it to the C packing directory.
Calling Wkhtmltopdf's command Runtime.getruntime (). EXEC ("C:\wkhtmltopdf.exe http://www.csdn.net c:\csdn.pdf") in Java enables the conversion.
The following encapsulates the command as a Java tool class for easy invocation.
Import Java.io.file;public class Htmltopdf {//wkhtmltopdf path in the system private static final String Topdftool = "C:\\wkhtmltopdf . exe ";/** * HTML to PDF * @param srcpath HTML path, either a path on the hard disk or a network path * @param destpath PDF Save path * @return Conversion successfully returns true */public St atic 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 (). 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 you receive input and error information for a process, you need to create additional threads, or the current thread waits (this behavior 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 get 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}}catch (ioexceptio n e) {e.printstacktrace ();}}}
Called in the servlet
/** * HTML to PDF */@WebServlet ("/htmltopdf/servlet") public class Htmltopdfservlet extends HttpServlet {private static Final long serialversionuid = 1l;/** * servlet receives parameter path, gets the URL of HTML */protected void service (HttpServletRequest request, Ht Tpservletresponse response) throws Servletexception, IOException {String path = request.getparameter ("path"); if (Path = = null | | Path.equals ("")) {return;} Get the temporary save path for the PDF//tmp for the directory under the website//put the generated PDF under the website in order to download string pdfpath = Request.getsession (). Getservletcontext (). Getrealpath ("/tmp"); String pdfname = Uuid.randomuuid (). toString () + ". pdf"; if (Htmltopdf.convert (path, Pdfpath + "/" + Pdfname)) { Response.sendredirect (Request.getcontextpath () + "/tmp/" + Pdfname);}}}
Enter the http://< Web site path in the browser >/htmltopdf/servlet?path=http://blog.csdn.net
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java implementation HTML to PDF