Recently, I encountered a very embarrassing problem at work. To convert HTML pages into PDF or tiff images and other forms, tiff is still confused. If you have a practical idea (do not copy it from someone else, the quality of Baidu's recent search has severely affected my confidence in Chinese products .) I hope I can tell you, thank you first.
Let's get down to the truth. Therefore, I converted HTML into PDF. Currently, it can perfectly support one-to-one conversion. I will post my solution below.
First of all, of course, it is to find the perfect component that can parse PDF, and Baidu and Google will tell us at the same time. Itext is king. Currently, among open-source components, itext is indeed a first choice, if you simply convert an image into a PDF or write the velocity or freemarker template to generate HTML, it is highly recommended that you use itext directly. If, like me, some people have already written HTML pages or are too lazy to write freemarker templates. You can directly look at the next section.
Because they have already written HTML pages and the display is perfect. What I want to do is parse the PDF generation tool of HTML + CSS perfectly. At this time, flying-saucer entered the scope of my selection.
Http://code.google.com/p/flying-saucer/
The above is the URL. This tool is hosted on googlecode. The author can do the following work:
Flying saucer takes XML or XHTML and applies CSS 2.1-compliant stylesheets to it, in order to render to PDF (via itext), images, and on-screen using swing or SWT.
It is easy to see that the working principle is to parse XML or XHTML and include CSS style sheets, and generate PDF functions using swing or SWT components. This solves the problem of page display. One of the serious problems of itext itself is that there is a big problem in parsing CSS. This solves the problem. The following code is implemented using flying saucer:
Public Boolean converthtmltopdf (string inputfile, string outputfile) throws exception {outputstream OS = new fileoutputstream (outputfile); itextrenderer Renderer = new itextrenderer (); string url = new file (inputfile ). touri (). tourl (). tostring (); Renderer. setdocument (URL); // solves the problem of Chinese support. itextfontresolver fontresolver = Renderer. getfontresolver (); fontresolver. addfont ("C:/Windows/fonts/simsun. TTC ", basefont. identity_h, basefont. not_embedded); // solves the problem of the relative path of the image Renderer. getsharedcontext (). setbaseurl ("file:/D:/"); Renderer. layout (); Renderer. createpdf (OS); OS. flush (); OS. close (); Return true ;}
The above code is like this. Enter an HTML url = inputfile and an output address to generate this PDF in the output PDF address.
Note:
1. The entered HTML page must be a standard XHTML page. The top of the page must be in the following format:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
In addition, the syntax of HTML pages must be very rigorous, and all tags must be closed. (because Flying-saucer does XML parsing, it will report an error if it is not rigorous .), This is the first requirement on the page.
2. Use the relative path of the image, for example:
The image location must be specified in Java code.
Renderer. getsharedcontext (). setbaseurl ("file:/D :/");
Another way is to write the absolute path directly in the label.
3. When flying-saucer parses an image in TIFF format, an error is returned. I have not found the specific cause. I hope you can give me some advice.
4. If there is a Chinese font in the page. CSS of a certain font must be written in the style in the HTML code, and must be in English. Then, the corresponding file location must be written in the Java code.
Itextfontresolver fontresolver = Renderer. getfontresolver ();
Fontresolver. addfont ("C:/Windows/fonts/simsun. TTC", basefont. identity_h, basefont. not_embedded );
The above method adds. You can also add other fonts.
The above is the solution.
The following are the packages. You can download it directly.
Click me!