How to generate a PDF report in JSP using IText

xiaoxiao2021-03-06  100

1. Background 2. Introduce ITEXT3. How to generate a PDF report 4 in a Java program using IText. How to generate PDF reports directly in JSP 1) Generate PDF files directly 2) Generating PDF file streams through the JSP 3) Generate the PDF file stream to the client 5 with the JSP 3) Generate the PDF file stream to the client 5 through the servlet. to sum up

1. background

Not long ago, a small project that generated a PDF report through JSP was counted. Some of the information of the company form an HTML report through the network. Although IE can print directly, it is displayed on the interface. If it is directly printed directly, it is not beautiful. If you turn it into a PDF file, print the printed effect will be much better. 2. ITEXT Introduction

IText is an open source Java class library that can easily generate a PDF file. Everyone downloads the latest version of the class library by visiting http://sourceforge.net/project/showfiles.net/project/showfiles.net/project/showfiles.net/project/showfiles.net/project/showfiles.net/project/showfiles.net/project/showfiles.net/project/showfiles.net/project/showfiles.net/project/ShowFiles.Net/project/ShowFiles.Net/iD=167948 gets a .jar package after downloading, add this package to the JDK ClassPath. If Chinese, Japanese, Korean characters need to appear in the generated PDF file, you also need to download the itemsian.jar package by accessing http://itext.sourceForge.Net/downloads/itextasian.jar. With regard to the use of the ITEXT class, http://www.lowagie.com/itext/tutorial/index.html has a more detailed tutorial. The tutorial starts from the entry, which is more systematically introduced in the PDF file, and the methods and techniques of writing, pictures, forms, etc. After reading this tutorial, you can do some from simple to complex PDF files. However, trying to resolve all difficulties encountered in the process of generating a PDF file through tutorial. So, the API document reading IText is very important. The reader can download the library document while downloading the class library. 3. How to generate a PDF report in the Java program with IText

The following is a simplest example in the above tutorial. This example portrays the general program framework for generating a PDF file through ITEXT. The reader only needs to be in Document.Open (); and Document.close (); the middle of the two statements will be added to the contents of the PDF file. This example only adds "Hello World" in the PDF file. Document document = new Document (); try {PdfWriter.getInstance (document, new FileOutputStream ( "Chap0101.pdf")); document.open (); document.add (new Paragraph ( "Hello World"));} catch ( DocumentException de) {system.err.println (de.getMessage ());} catch (ioException ie) {system.err.println (IOE.GETMESSAGE ());} Document.close ();

As can be seen from the above example, the framework of the program is clear. However, in the PDF, the location of the text, drawing, and form is a very troublesome thing. In addition to constantly modifying location, then run the program, generating a PDF file, observing whether the location of the element is reasonable in the PDF, there seems to have no other better ways. 4. How to generate PDF reports through JSP is not available in IText tutorial, online related information is relatively small. I have seen someone on 9CBS to ask for details, some people replied to the principle of implementation: first generate PDF files on the server, and then the user selects the download or open by clicking the hyperlink pointing to the PDF file. This is a idea, or one of the ideas. This article has achieved this idea, and another idea is given and implemented by two ways. 1) Generate a PDF file directly on the server. <% @ Page Import = "com.lowagie.text. *, com.lowagie.text.pdf. *, java.io. *"%> <% string filename = "pdf" (new random ()). Nextint () ". pdf"; Document document = new Document (PageSize.A4); ServletOutputStream out1 = response.getOutputStream (); try {PdfWriter writer = PdfWriter.getInstance (document, new FileOutputStream (filename)); document.open ( ); Document.Add (New Paragraph ("Hello World"); Document.close ();} catch (Exception E) {}%>

The above program generates a static PDF file on the server. Obviously, the name of the PDF file that runs the resulting PDF should be unique. This program names the generated PDF file through a random function. The disadvantage of this program is that each run will generate a PDF file on the server. If it is not deleted in time, the quantity will be getting bigger and bigger, which is obviously unwilling to see the site maintenance. 2) Transfer the PDF file through the flow form to the client's cache. The benefits of doing this are not to leave any "remains" on the server.

i) Generate <% @ page import = "java.io. *, java.awt.color, com.lowagie.text. *, com.lowagie.text.pdf. *"%> ii) Generate import java.io. *; Import javax.servlet. *; Import javax.servlet.http . *; import com.lowagie.text *;. import com.lowagie.text.pdf *;. public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {Document document = new Document (PageSize.A4, 36, 36, 36, 36); ByteaRrayoutputStream Ba = new byteArrayoutputStream () ; Try {PdfWriter writer = PdfWriter.getInstance (document, ba); document.open (); document.add (new Paragraph ( "Hello World"));} catch (DocumentException de) {de.printStackTrace (); System. Err.Println ("A Document Error:" de.getMessage ();} Document.close (); response.setContentType ("Application / PDF"); response.setContentLength; servletOutstream out = Response.getOutputStream (); ba.writeto (out); out.flush ();} 5. End I use a second method in the project. The source code of this article is debugged in my Tomcat4. I hope I can bring convenience to you.

转载请注明原文地址:https://www.9cbs.com/read-97553.html

New Post(0)