How to generate a PDF report in JSP with IText -

xiaoxiao2021-03-06  61

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 portrayes the generals of generating a PDF file through IText

Program frame. 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.

4. 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.

<% @ PageImport =

"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 write = 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

%>

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 ();

}

5. 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.

Reprinted: Java Research Organization: Moneyqb

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

New Post(0)