Generate a PDF full guide, pdf full Guide

Source: Internet
Author: User

Generate a PDF full guide, pdf full Guide

How many pitfalls have been experienced, how many similar blogs have been viewed, and how many versions of Jar have been downloaded. In order to find the correct code writing method, the function points in the project manager requirement analysis book can be realized.

This article describes the bumpy development path of programmers in small and medium-sized companies by using Java EE to generate PDF files.

The reason why I only excluded large mature companies is that large companies generally have mature product lines and technical accumulation.

At least there will be a well-developed constructor team, a cool hermit like a monk who sweeps the floor ......

When junior programmers in the company encounter problems, they can find code that has previously implemented similar functions as a reference and consult the technical manager.

Small companies do not have this blessing, just as yesterday morning's "generate PDF" requirement, the construction period is one day, tomorrow to show the customer.

The first reaction is to find du Niang, the keyword "Java generate PDF". There are not many blogs. The following describes several implementation methods and problems encountered in the implementation process.

(The Directory is already listed on it. If you come in through the search engine, check whether the list above can solve your problem. It's a bit like a corresponding section, close the next search record ....)

1. generate complex PDF files using IText

The largest proportion of DU Niang's Java-generated PDF implementation class library is also involved in many technical blogs.

Introduction to open-source China: http://www.oschina.net/p/itext. For more information, see these libraries.

Download Jar is also a wide array of contents, so you can find everything from 2.1 to 5.5. Some forums also need to register o (register multiple items) o alas.

The latest version 5.5 is used here. read other people's technical blogs or official documents carefully and it is not very difficult to code them. The following is a class of abstract business implementation:

Import com. itextpdf. text. document; import com. itextpdf. text. element; import com. itextpdf. text. font; import com. itextpdf. text. phrase; import com.itext;.text=. baseFont; import com.itext;.text=. export pcell; import com.itext;.text=. pdfPTable; import com.itext;.text=. using writer; import java. io. fileOutputStream; public class createSimplePDF {private static Font FontChinese; public static void simplePDF () {try {BaseFont bfChinese = BaseFont. createFont ("STSong-Light", "UniGB-UCS2-H", BaseFont. NOT_EMBEDDED); FontChinese = new Font (bfChinese, 12, Font. NORMAL); Document document = new Document (); author writer. getInstance (document, new FileOutputStream ("F :\\ Garbage \ Hello simplemediaworkflow"); document. open (); PdfPTable table = new PdfPTable (4); table. addCell (getCell ("name", 1, 1); table. addCell (getCell ("", 1, 1); table. addCell (getCell ("Number", 1, 1); table. addCell (getCell ("", 1, 1); table. addCell (getCell ("department", 1, 1); table. addCell (getCell ("", 1, 1); table. addCell (getCell ("job name", 1, 1); table. addCell (getCell ("", 1, 1); table. addCell (getCell ("date of assignment", 1, 1); table. addCell (getCell ("", 1, 1); table. addCell (getCell ("date of departure", 1, 1); table. addCell (getCell ("", 1, 1); table. addCell (getCell ("cause", 1, 3); table. addCell (getCell ("", 3, 3); table. addCell (getCell ("department comment", 1, 3); table. addCell (getCell ("", 3, 3); document. add (table); document. close ();} catch (Exception e) {e. printStackTrace () ;}} public static response pcell getCell (String cellValue, int colspan, int rowSpan) {response pcell cell = new response pcell (); try {cell = new PdfPCell (new Phrase (cellValue, FontChinese); cell. setRowspan (rowSpan); cell. setColspan (colspan); cell. setHorizontalAlignment (Element. ALIGN_CENTER);} catch (Exception e) {e. printStackTrace () ;}return cell ;}}
2. added Chinese support for IText

After the code is compiled, the Chinese characters are lost. Note that the Code is not garbled and the error is as follows:

Font 'stsong-light' with 'unigb-UCS2-H 'is not recognized

Ask du Niang again, found that IText needs to add another itext-asian.jar to support Chinese, then add it.

Because IText is using the latest version 5.5, other earlier versions of itext-asian.jar cannot be supported.

In the end, I found a solution in a blog. The solution worked after the attempt, in the above Code:

 BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); FontChinese = new Font(bfChinese, 12, Font.NORMAL);

Here I will be the latest IText. jar and supporting Chinese support itext-asian.jar into: http://pan.baidu.com/s/1bqs 4km extraction password: 99eg

Save the jar search time for later students. The implementation is as follows:

In fact, the final form to be implemented is much more complicated than this one. 80% of the time spent is mainly adjusting the style and compiling the output ---> looking at the style, the requirement has been realized here.

This method is also implemented by the author. After all, it is demonstrated to the customer.

Here, I would like to thank this blogger's blog, which contains detailed IText Settings section, title, table, encryption .......... as long as you can think of generating PDF files, there are basically.

Blog: http://rensanning.iteye.com/blog/1538689

3. iTextRenderer (Flying Saucer) HTML to PDF

Flying saucer is the name of an open-source project. I am also drunk when a foreigner starts to name it. I think of it as an "aircraft" When HTML Rendering PDF ".

The core class iTextRenderer supports generating html pdf files.

ITextRenderer depends on the Renderer independently implemented by iText. It can basically achieve the integrity of CSS 2.1 and fully comply with W3C specifications.

If you use this method to adjust the style or something in the background, let it go to hell.

The specific process is as follows:

This is a great solution. The template engine is also very wide (freemark, velocity ......). For more information, see your project.

In this way, you don't have to worry about complicated styles. You can define the front-end view of templates, inject data, and generate PDF files.

                ITextRenderer iTextRenderer = new ITextRenderer();                iTextRenderer.getFontResolver().addFont("C:/Windows/Fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);                iTextRenderer.setDocument(new File(currWebcontentPath + reviewHtmlPath).toURI().toString());                iTextRenderer.layout();                String pdfName = getPdfName(testVO);                OutputStream fileOutputStream = new FileOutputStream(currWebcontentPath + pdfPath + pdfName);                iTextRenderer.createPDF(fileOutputStream);                iTextRenderer.finishPDF();

The problem that needs to be solved here is the location where the generated HTML is stored, and then jump to the download page. If you are a Java EE backend development, these problems should not be yours.

The jar of iTextRenderer used is also put in: http://pan.baidu.com/s/1kTOpM0R extract password: y9y2

For details, refer to the following blogs:

Http://www.tuicool.com/articles/qAFNFja

Http://downpour.iteye.com/blog/509417

Http://my.oschina.net/u/603602/blog/268611? Fromerr = bxBuHc6W

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.