Recent projects need to do an online contract function, the contract has electronic version, the need to put the web version of the contract directly production of PDF, the requirements of the style can not be disorderly, there are page numbers have headers and footers, and so on, online search for a period of time, found a tool called: wkhtmltopdf , This should be a foreign free software, there is a windows,mac,linux version, directly in the command line calls, these days completed a demo, incidentally summed up.
This is the tool's official website: the tools required by http://wkhtmltopdf.org/downloads.html can be downloaded here, free of charge.
March 7, 2017 15:49:58 as if the tool could not parse angularjs ...
Put the demo code first:
Calling class:
Package wkhtmltopdf;
Import Java.io.File;
Import Java.util.Date;
/** * Created by Jerry in 2017/2/13. * God Bless Me * * public class Wkhtmltopdf {//wkhtmltopdf path in system private static final String Topdftool = "c:\\
Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe ";
public static void Main (string[] args) {Date date = new Date ();
String fileName = string.valueof (Date.gettime ()) + ". pdf";
Convert ("Http://www.jianshu.com/p/74e3a6ee4ae8", "d:\\pdf\\" + fileName); /** * HTML to PDF * * * @param srcpath HTML path, can be the path on the hard disk, can also be network path * @param destpath PDF Save Path * Return conversion successfully returns TRUE */public static Boolean convert (String Srcpath, String destpath) {File File = new Fil
E (DestPath);
File parent = File.getparentfile ();
If the PDF save path does not exist, create the path if (!parent.exists ()) {parent.mkdirs ();
} StringBuilder cmd = Getformal ();
StringBuilder cmd = test1 (); The HTML path is the target page path Cmd.append ("");
Cmd.append (Srcpath);
Cmd.append ("");
PDF Save Path 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; /** * Standard Format * * @return/public static StringBuilder Getformal () {StringBuilder
cmd = new StringBuilder ();
Wkhtmltopdf path Cmd.append (topdftool) in the system;
Cmd.append (""); Cmd.append ("--header-line");//The line below the header//Cmd.append ("--footer-line");//footer above line Cmd.append ("--footer-center [Page]/[topage]"); Place page number//Cmd.append in the footer Center ("--header-right here is the header of our system"); Place the text cmd.append in the middle of the header ("--header-html http://localhost:8090/myheader.html"); In the middle of the header place picture Cmd.append ("--header-spacing 5");//(Set the distance between header and content, default 0) cmd.append ("--margin-top 20mm");
Sets the top margin of the page (default 10mm) cmd.append ("Cover http://localhost:8090/firstPage.html");
return cmd;
public static StringBuilder Test1 () {StringBuilder cmd = new StringBuilder ();
Wkhtmltopdf path Cmd.append (topdftool) in the system;
Cmd.append ("--cover http://localhost:8090/firstPage.html");
Cmd.append ("T 15mm");
Cmd.append ("--header-spacing 5");
Cmd.append ("--outline");
Cmd.append ("Cover http://image.baidu.com");
Cmd.append ("");
return cmd; }
}
Tool Class:
Package wkhtmltopdf;
/**
* Created by Jerry on 2017/2/13.
* God Bless Me * *
import java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
/**
* When Java calls Wkhtmltopdf, to get the content returned by wkhtmltopdf/public
class Htmltopdfinterceptor extends Thread {
private InputStream is;
Public Htmltopdfinterceptor (InputStream are) {
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.print (line.tostring ());//output Content
}
}catch ( IOException e) {
e.printstacktrace ();}}}
Basically you copy these two classes, install the tool, you can run directly.
Here are a few of my own experiences to explore:
Although I look at a few of the data there is no say command execution has a distinction, but it is obvious that in the generation of the first page, you must
Cmd.append ("Cover http://localhost:8090/firstPage.html");
Write in the end, otherwise it will be an error, if you know the reason, you can contact me qq:527397749
There is also a bit easy to ignore, is the last of each command must add space, otherwise there will be the problem of command adhesion, very basic, it is difficult to find.
A list of commonly used commands is attached;
linux:wkhtmltopdf [OPTIONS] ... [more input files] windows:wkhtmltopdf.exe [OPTIONS] ... [more input files] The General option –allow allows loading of files or files from a specified folder (repeatable) –book* settings to print a book one time, usually set the options –collate print multiple copies when sorting –cookie set an extra Cookies (repeatable) –cookie-jar read and write cookies, and –copies copies of the cookie jar file provided are printed to the number of PDF files (default 1) –cover* use HTML files as cover pages. It will take the header and footer of the TOC before inserting –custom-header to set an additional HTTP header (REPEATABLE) –debug-javascript display the JavaScript debug output –default-header* Add a default header, The name to the left of the page, the number of pages to the right, for example: –header-left ' [webpage] ' –header-right ' [page]/[topage] ' –header-line–disable-external-links* Prevent build links to remote Web pages –disable-internal-links* prohibit the use of local links –disable-javascript prevent web pages from being executed javascript–disable-pdf-compression* Prohibit the use of lossless compression in PDF objects –disable-smart-shrinking* prohibit the use of WebKit intelligent strategic shrinkage, so that the pixel/DPI ratio is not unchanged –disallow-local-file-access Prevents local files that are allowed to be converted from reading other local files unless explecitily allows –allow–dpi to be explicitly changed by the DPI (which has no effect on X11 based systems) –enable-plugins enable installed plug-ins such as flash–encoding Setting the default text encoding –extended-help displays a wider range of help, detailing unusual command switches –forms* open HTML form fields to PDF form Field –grayscale PDF format will be generated in grayscale –help display helP–htmldoc Output program HTML Help –ignore-load-errors ignore Claimes load process has encountered an error page –lowquality produces a low quality pdf/ps. Useful space for narrowing results documents –manpage the Output manual page –margin-bottom set the page bottom margin (default 10mm) –margin-left the left margin (default 10mm) –margin-right settings Page right margin (default 10mm) –margin-top sets the top margin of the page (default 10mm) –minimum-font-size minimum font size (default 5) –no-background does not print background –or Ientation set orientation to Horizontal or portrait –page-height page height (default unit millimeter) –page-offset* Set start Page number (default 1) –page-size set paper size: A4
, letter, etc. –page-width page width (default unit millimeter) –password HTTP authentication Password –post Add an additional post field (repeatable) –post-fil e Post an aditional file (repeatable) –print-media-type* the type of print media used, instead of screen –proxy using proxy –quiet be less Verbose–read-args -from-stdin read standard input command-line arguments –readme renderer –redirect-delay wait milliseconds for js-redirect (default 200) –replace* replace name, value header and footer (repeatable) –STOP-SL Ow-scripts stop running slow javascripts–title the title of the generated PDF file (the first document title is used, if not specified) –toc* the beginning of the file in the table of the inserted content –use-xserver* Using the X server (some plugins and other things without X11 may not work) –user-style-sheet specifies the user's style sheet, loads the –username HTTP-authenticated username –version output version information in each page exit –zoom Use this scaling factor (default 1) Header and footer options –header-center* (set in central location
Header content) –header-font-name* (default Arial) (Set header font name) –header-font-size* (set header font size) –header-html* (add an HTML header, followed by the URL) –header-left* (left-aligned header text) –header-line* (displays a line under the header) –header-right* (right-aligned header text) –header-spacing* (set header and content distance, default 0) –footer- center* (set footer content in center) –footer-font-name* (set footer font name) –footer-font-size* (set footer font size default 11) –footer-html* (add an HTML
Footer, followed by URL) –footer-left* (left aligned footer text) –footer-line* display a line on the footer content) –footer-right* (right-aligned footer text) –footer-spacing* (set the distance between footer and content) ./wkhtmltopdf–footer-right ' [page]/[topage] ' http://www.baidu.com baidu.pdf./wkhtmltopdf–header-center ' statements '- Header-line–margin-top 2cm–header-line http://192.168.212.139/oma/oma.pdf Table content options –toc-depth* Set the depth the TOC (default 3) –toc-disable-back-links* do don't link from section header to toc–toc-disable-links* does not link from TOC to Sections–toc-font-name* Set the Font used for the TOC (default Arial) –toc-header-font-name* the font of the TOC header (if unset use–toc-font-name) – toc-header-font-size* the font size of the TOC header (default 15) –toc-header-text* the header text of the TOC (default Table of Contents) –toc-l1-font-size* set the font size on level 1 of the TOC (default 12) –toc-l1-indentation* set Inde Ntation on Level 1 of the TOC (default 0) –toc-l2-font-size* Set the font size to level 2 of the TOC (default 10) –toc-l 2-indentation* set indentation on level 2 of the TOC (default 20) –toc-l3-font-size* Set the font size to level 3 of the TOC (default 8) –toc-l3-indentation* set indentation on level 3 of the TOC (default 40) –toc-l4-font-size* set the font Size on level 4 of the TOC (default 6) –toc-l4-indentation* Set indentation at level 4 of the TOC (default 60) –toc-l5-f ont-size* set the font size on level 5 of the TOC (default 4) –toc-l5-indentation* set indentation at level 5 of the TOC (Default 80) –toc-l6-font-size* set the font size on level 6 of the TOC (default 2) –toc-l6-indentation* set indentation to Level 6 of the TOC ( Default 100) –toc-l7-font-size* set the font size on level 7 of the TOC (default 0) –toc-l7-indentation* set indentation On level 7 of the TOC (default 120) –toc-no-dots* don't use dots, in the TOC contour option –dump-outline Dump directory to a file –outline Table of Contents (H1,h2 in the article) –outline-depth set the depth of the directory (default is 4) footer and header * [Page] replaces the number of pages currently being printed with * [FromPage] Replaced by the number of the first page to be printed * [ToPage] by the last The number of pages to be printed replaces * [webpage] replaces by the URL of the page being printed * [section] replaced by the name of the current section * [subsection] replaced by the name of the current subsection * [Date] is replaced by the local format of the current date system * [Time Replaced by the current time, the system's local format
There is a good article: http://www.jianshu.com/p/4d65857ffe5e Everyone can read together, other materials I think there is nothing to see, because I have read it again.
Welcome message discussion.