Recent projects need to export Enterprise Risk report, file format for pdf, so searched a large number of articles are what Jasper report,IText ,flying Sauser , have tried again, Feeling is not the effect I want,
Need to adjust their own a lot of things, style, font and so on, good trouble, after all, Bo master so lazy people, haha. -------Jstarseven
Anyway
Java generated PDF file, a lot of ways, the above mentioned in which ways specific can refer to this article < explain very detailed >:http://blog.csdn.net/ouyhong123/article/details/26401967
This article says the blogger thinks it is better to use a tool: Wkhtmltopdf
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, easy to use, at least my PDF is not super complex, haha
: https://wkhtmltopdf.org/downloads.html
Install after download, bo master installation directory: E:\software\wkhtmltopdf
First Test:
[Reprint please indicate the original source]:http://www.cnblogs.com/jstarseven/p/7251711.html, so many words, Bo main code is also very tired, thank you for your cooperation.
Enter CMD,CD E:\software\wkhtmltopdf\bin
Execute Wkhtmltopdf.exe http://www.jstarseven.com/e:\data\pdf\jstarseven.pdf
Open E:\data\pdf\jstarseven.pdf look at the effect can also, at least not very troublesome.
Then it is to write a small program, encapsulate it, tune the DOS command.
The blogger's idea is to prepare an HTML template to generate the PDF, the data is all placeholder placeholder, when the need to generate a PDF, the first to write data to the file, and then call Wkhtmltopdf to generate a PDF file.
The part of the document does not say, look directly at the PDF part of it, the code is very simple.
Code: Htmltopdf.java
1 Packagecom.server.wkhtmltopdf;2 3 ImportJava.io.*;4 5 /**6 * Created by Jstarseven on 2017/7/25.7 */8 Public classHtmltopdf {9 //wkhtmltopdf path in the systemTen Private Static FinalString Topdftool = "D:\\software-dev\\wkhtmltopdf\\bin\\wkhtmltopdf.exe"; One A /** - * HTML to PDF - * the * @paramsrcpath HTML path, either a path on the hard disk or a network path - * @paramdestpath PDF Save Path - * @returnConversion succeeded return true - */ + Public Static BooleanConvert (String srcpath, String destpath) { -File File =NewFile (destpath); +File parent =file.getparentfile (); A //if the PDF save path does not exist, the path is created at if(!parent.exists ()) - parent.mkdirs (); -StringBuilder cmd =NewStringBuilder (); - cmd.append (topdftool); -Cmd.append (""); - cmd.append (srcpath); inCmd.append (""); - cmd.append (destpath); to + Booleanresult =true; - Try { theProcess proc =runtime.getruntime (). EXEC (cmd.tostring ()); *Htmltopdfinterceptor error =NewHtmltopdfinterceptor (Proc.geterrorstream ()); $Htmltopdfinterceptor output =NewHtmltopdfinterceptor (Proc.getinputstream ());Panax Notoginseng Error.start (); - Output.start (); the proc.waitfor (); +}Catch(Exception e) { Aresult =false; the e.printstacktrace (); + } - $ returnresult; $ } - - Public Static voidMain (string[] args) { theConvert ("http://www.jstarseven.com/", "E:\\data\\pdf\\jstarseven.pdf"); - }Wuyi } the - classHtmltopdfinterceptorextendsThread { Wu PrivateInputStream is; - About PublicHtmltopdfinterceptor (InputStream is) { $ This. is =is ; - } - - Public voidrun () { A Try { +InputStreamReader ISR =NewInputStreamReader (IS, "Utf-8"); theBufferedReader br =NewBufferedReader (ISR); - String Line; $ while(line = Br.readline ())! =NULL) { theSystem.out.println (line);//Output Content the } the}Catch(IOException e) { the e.printstacktrace (); - } in } the}View Code
Effect Test Chart:
Finally reached the effect, in fact, and the use of PHANTOMJS cut the figure almost, personal feeling is good, do not know if there will be other problems, yet to be found ...
[Reprint please indicate the original source]:http://www.cnblogs.com/jstarseven/p/7251711.html, so many words, Bo main code is also very tired, thank you for your cooperation.
Java call wkhtmltopdf generate PDF file, beautiful, convenient