Generate PDF reports in JSP with IText

zhaozj2021-02-16  52

Origin of the problem

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.

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.

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 IOE)

{

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.

How to generate a PDF report through JSP

This part is not there in IText tutorial, and there is relatively small online information. 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 directly through JSP page

<% @

Page Import = "java.io. *, java.awt.color, com.lowagie.text. *, com.lowagie.text.pdf. *"%>

<%

Response.setContentType ("Application / PDF");

Document Document = New Document ();

BYTEARRAYOUTPUTSTREAM BUFFER = New byteArrayoutputStream ();

Pdfwriter Writer = pdfwriter.getInstance (Document, Buffer);

Document.open ();

Document.Add (New Paragraph ("Hello World"));

Document.close ();

DataOutput output = new DataOutputStream (response.getoutputstream ());

BYTE [] bytes = buffer.tobytearray ();

Response.setContentLength; Bytes.length;

For (int i = 0; i

{

Output.writebyTe (bytes [i]);

}

%>

II) Generate through servlet

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; Ba.Size ());

ServletOutputStream out = response.getOutputStream ();

Ba.Writeto (OUT);

Out.flush ();

}

end

I use the 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-20520.html

New Post(0)