Java wkhtmltopdf to convert Html to PDF, wkhtmltow.pdf
As we all know in java development, most java-generated pdf files 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. So I found a tool wkhtmltopdf that can be directly called to convert the generated html to pdf directly. Powerful functions.
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 to pdf is very simple, as long as you enter c: \ wkhtmltow..exe http://www.cnblogs.com c: \ cnblogs.pdf in the windows Command Line
You can convert the blog garden webpage into a pdf and save it to the C root directory.
In Java, you can call the wkhtmltopdfcommand runtime.getruntime(cmd.exe c ("c: \ wkhtmltopdf.exe http://www.cnblogs.com c: \ cnblogs.pdf") to implement the conversion.
The following command is encapsulated into a java tool class for convenient calling.
/*** Copyright (C) 2016 www. xiongge. club * @ author xsw * @ date 2016-12-8 am 10:17:33 */package wkhtmltopdf; import java. io. bufferedReader; import java. io. IOException; import java. io. inputStream; import java. io. inputStreamReader;/*** @ ClassName: htmltow.interceptor * @ Description: TODO () * @ author xsw * @ date 2016-12-8 10:17:33 **/public class htmltow.interceptor 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. out. println (line. toString (); // output content} catch (IOException e) {e. printStackTrace ();}}}
/*** Copyright (C) 2016 www. xiongge. club * @ author xsw * @ date 2016-12-8 am 10:14:54 */package wkhtmltopdf; import java. io. file;/*** @ ClassName: HtmlToPdf * @ Description: TODO () * @ author xsw * @ date 10:14:54 **/public class HtmlToPdf {// path of wkhtmltopdf in the system private static final String tow.tool = "D: \ wkhtmltopdf \ bin \ wkhtmltocmd.exe ";/*** convert html to pdf * @ param srcPath html path, which can be the path on the hard disk or the network Path * @ param destPath pdf save path * @ return returns true after successful conversion */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 ("-- header-line"); // line cmd under the header. append ("-- header-center here is the header"); // The middle content of the header // cmd. append ("-- margin-top 30mm"); // sets the top margin of the page (default 10mm) cmd. append ("-- header-spacing 10"); // (sets the distance between the header and content. The default value is 0) 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;} public static void main (String [] args) {HtmlToPdf. convert ("http://www.cnblogs.com/xionggeclub/p/6144241.html", "d:/wkhtmltopdf.pdf ");}}
Attached wkhtmltopdf Parameter Details
Wkhtmltopdf [OPTIONS]... <input file> [More input files] <output file> General options -- allow <path> allow loading files or files from the specified folder (repeated) -- book * When printing a book for a while, the usual option -- collate is used to print multiple copies -- cookie <name> <value> sets an additional cookie (repeatable) -- cookie-jar <path>: the Cookie that is read and written, and printed as a PDF file in the provided cookie jar file -- copies <number> (1 by default) -- cover * <url> uses an HTML file as the cover. It inserts -- custom-header <name> <value> to the TOC with the header and footer to set an additional HTTP header (repeated) -- debug-javascript: The javascript debugging output -- default-header * adds a default header, with the name on the left of the page. The page is counted to the right, for example: -- header-left '[webpage]' -- header-right '[page]/[toPage]' -- header-line -- disable-external-links * prohibit generation of links to remote webpages -- disable -internal-links * prohibit the use of local links -- disable-javascript prohibit the execution of JavaScript -- disable-pdf-compression * on webpages prohibit the use of lossless compression on PDF objects -- disable-smart-shrink Ing * prohibits the use of WebKit smart strategic contraction, so that the pixel/DPI ratio does not remain unchanged -- disallow-local-file-access prohibits the conversion of local files to read other local files, unless explecitily allows explicit changes to dpi with -- allow -- dpi <DPI> (this has no impact on X11-based systems) -- enable-plugins enable installed plug-ins (for example, Flash -- encoding <encoding> sets the default text encoding -- extended-help to display more extensive help, detailed introduction to the uncommon command switch -- forms * Turn on HTML form fields to PDF form fields -- grayscale PDF format will be generated in grayscale -- help Display help -- HTML Doc output program HTML help -- ignore -The load-errors has encountered an error page -- lowq Uality produces low quality PDF/PS. Used to narrow the space of the result document-manpage output program manual page-margin-bottom <unitreal> set the page bottom margin (default 10mm) -- margin-left <unitreal> place the left margin (default 10mm) -- margin-right <unitreal> to the right margin of the page (default 10mm) -- margin-top <unitreal> set the top margin of the page (default 10mm) -- minimum-font-size <int> minimum font size (default 5) -- no-background: do not print the background -- orientation <orientation> sets the direction to horizontal or vertical -- page-height <unitreal> page height (default unit millimeter) -- page-offset * <offset> set the starting page number (default 1) -- page-size <size> set the paper size: A4, Letter, etc. -- page-width <unitreal> page width (default unit millimeter) -- password <password> HTTP verification password -- post <name> <value> Add an additional post field (repeatable) -- post-file <name> <path> Post an aditional file (repeatable) -- print-media-type * specifies the print media type, instead of the screen -- proxy <proxy> use proxy -- quiet Be less verbose -- read-args-from-stdin to read standard input command line parameters -- readme output program self-report -- redirect-delay <msec> wait for several milliseconds for JS-redirection (default 200) -- replace * <name> <value> replace the name. The header and footer of the value can be repeated) -- stop-slow-scripts: the title of the PDF file generated by the slow execution of JavaScripts -- title <text> (if not specified, the title of the first document is used) -- toc * The Beginning of the file in the inserted content table -- use-xserver * uses X Server (some plug-ins and other things may not work normally without X11) -- user-style-sheet <url> specifies the user's style sheet, load -- username <username> HTTP authenticated username -- version output version information on each page to exit -- zoom <float> use this scaling factor (default 1) header and footer options -- header-center * <text> (set the header content in the center) -- header-font-name * <name> (default Arial) (set the header font name) -- header-font-size * <size> (set the header font size) -- header-html * <url> (add an HTML header, followed by the URL) -- header-left * <text> (left-aligned header text) -- header-line * (display a line under the header) -- header-right * <text> (right-aligned header text) -- header-spacing * <real> (sets the distance between the header and content. The default value is 0) -- footer-center * <text> (set the footer content in the center) -- footer-font-name * <name> (set the font name of the footer) -- footer-font-size * <size> (set the font size of the footer to default 11) -- footer-html * <url> (add an HTML footer followed by a url) -- footer-left * <text> (left-aligned footer text) -- footer-line * displays a line on the footer content) -- footer-right * <text> (right-aligned footer text) -- footer-spacing * <real> (set the distance between footer and content ). /wkhtmltopdf -- footer-right '[page]/[topage]' http://www.baidu.com baidu.pdf. /wkhtmltopdf -- header-center 'report' -- header-line -- margin-top 2 cm -- header-line http: // In the content option of the 192.168.212.139/oma/oma.pdf table -- toc-depth * <level> Set the depth of the toc (default 3) -- toc-disable-back-links * Do not link from section header to toc -- toc-disable-links * Do not link from toc to sections -- toc-font-name * <name> Set the font used for the toc (default Arial) -- toc-header-font-name * <name> The font of the toc header (if unset use -- toc-font-name) -- toc-header-font-size * <size> The font size of the toc header (default 15) -- toc-header-text * <text> The header text of the toc (default Table Of Contents) -- toc-l1-font-size * <size> Set the font size on level 1 of the toc (default 12) -- toc-l1-indentation * <num> Set indentation on level 1 of the toc (default 0) -- toc-l2-font-size * <size> Set the font size on level 2 of the toc (default 10) -- toc-l2-indentation * <num> Set indentation on level 2 of the toc (default 20) -- toc-l3-font-size * <size> Set the font size on level 3 of the toc (default 8) -- toc-l3-indentation * <num> Set indentation on level 3 of the toc (default 40) -- toc-l4-font-size * <size> Set the font size on level 4 of the toc (default 6) -- toc-l4-indentation * <num> Set indentation on level 4 of the toc (default 60) -- toc-l5-font-size * <size> Set the font size on level 5 of the toc (default 4) -- toc-l5-indentation * <num> Set indentation on level 5 of the toc (default 80) -- toc-l6-font-size * <size> Set the font size on level 6 of the toc (default 2) -- toc-l6-indentation * <num> Set indentation on level 6 of the toc (default 100) -- toc-l7-font-size * <size> Set the font size on level 7 of the toc (default 0) -- toc-l7-indentation * <num> Set indentation on level 7 of the toc (default 120) -- toc-no-dots * Do not use dots, in the toc profile option -- dump-outline <file> dump directory to a file -- outline display directory (h1, (h2) -- outline-depth <level> specifies the directory depth (4 by default) the footer and header * [page] are replaced by the number of pages currently being printed * [frompage] is replaced by the number of the first page to be printed * [topage] is replaced by the number of pages to be printed on the last page quantity replace * [webpage] replace * [section] with the name of the current section by the URL of the page being printed * [subsection] with the name of the current section * [date] with the current section the local format of the date system replaces * [time] by the current time, replace the local format of the system