How to Use itext to generate a PDF report in JSP --

Source: Internet
Author: User
Document directory
  • 1. Background
  • 2. Introduction to itext
  • 3. How to Use itext to generate a PDF report in a Java program
  • 4. How to generate a PDF report through JSP
  • 5. End
1. Recently, a small project was created to generate PDF reports through JSP, opening up an eye. Some enterprise information forms HTML reports through the network. Although Ie can directly print the content displayed in the reports, from the perspective of the interface, if the HTML display result is printed directly, not very nice. If you convert it into a PDF file and then print it, the printing effect will be much better. 2. itext introduction itext is an open-source Java class library that can be used to easily generate PDF files. By accessing http://sourceforge.net/project/showfiles.php? Group_id = 15255 & release_id = 167948 download the latest version of the class library. After the download is complete, you will get a. jar package. Add this package to the JDK classpath for use. If the generated PDF file contains Chinese, Japanese, and Korean characters, access http://itext.sourceforge.net/downloads/itextasian.jarto download the itextasian.jarpackage.
For more information about the use of the itext class library, visit http://www.lowagie.com/itext/tutorial/index.html. This tutorial describes how to add text, images, and tables to a PDF file. After reading this tutorial, you can make some PDF files from simple to complex. However, it is a luxury to try to solve all the difficulties encountered in the process of generating PDF files through the tutorial. Therefore, it is very important to read the API documentation of itext. When downloading the class library, you can also download the documentation of the class library. 3. How to Use itext to generate a PDF report in a Java program the following is a simple example in the above tutorial.
Program Framework. You only need to add the content you want to put in the PDF file between document. open (); and document. Close. In this example, only a line of "Hello world" is added to the PDF file.
Document document = NewDocument ();
Try{
Using writer. getinstance (document, NewFileoutputstream ("chap0101.pdf "));
Document. open ();
Document. Add ( NewParagraph ("Hello World "));
}
Catch(Incluentexception de ){
System. Err. println (De. getmessage ());
}
Catch(Ioexception IOE ){
System. Err. println (IOE. getmessage ());
}
Document. Close ();

The above example shows that the program framework is very clear. However, it is very troublesome to specify the positions of text, pictures, and tables in PDF. In addition to constantly changing the position in the program, then running the program, generating a PDF file, and observing whether the position of the element in the PDF is reasonable, there seems to be no better way. 4. How to generate a PDF report using JSP is not found in the itext tutorial, and there are few online documents. I once saw someone posting on csdn asking for implementation details, and someone replied to the implementation principle: first generate a PDF file on the server, and then the user chooses to download or open it by clicking the hyperlink pointing to the PDF file. This is an idea, or an idea. This article implements this idea and provides another idea and implements it in two ways.
1) generate a PDF file directly on the server.
<% @ PageImport= "Com. lowagie. Text. *, com.lowagie.text=. *, java. Io. *" %>
<%
String filename = "pdf" + (NewRandom (). nextint () + ". pdf ";
Document document =NewDocument (pagesize. A4 );
Servletoutputstream out1 = response. getoutputstream ();
 Try{
Using writer = Using writer. getinstance (document,NewFileoutputstream (filename ));
Document. open ();
Document. Add (NewParagraph ("Hello World "));
Document. Close ();
}
Catch(Exception e ){}
%>

The above program generates a static PDF file on the server. Obviously, the names of the PDF files obtained from each operation should be unique and cannot be duplicated. This program uses a random function to name the generated PDF file. The disadvantage of this program is that every operation will generate a PDF file on the server. If it is not deleted in time, the number will increase, which is obviously not desired by the site maintainer.
2) Transfer the PDF file to the client cache in the form of a stream. The advantage of doing so is that it will not leave any "relics" on the server ".
(I) generate the file directly through the JSP page.
<% @
PageImport= "Java. Io. *, java. AWT. Color, Com. lowagie. Text. *, com.lowagie.text=. *" %>
<%
Response. setcontenttype ("application/pdf ");
Document document =NewDocument ();
Bytearrayoutputstream buffer =NewBytearrayoutputstream ();
Response writer = Response writer. getinstance (document, buffer );
Document. open ();
Document. Add (NewParagraph ("Hello World "));
Document. Close ();
Dataoutput output =NewDataoutputstream (response. getoutputstream ());
Byte[]ByteS = buffer. tobytearray ();
Response. setcontentlength (bytes. Length );
For(IntI = 0; I <bytes. length; I ++) {output. writebyte (Bytes [I]);}
%>
Ii) generate through Servlet
ImportJava. Io .*;
ImportJavax. servlet .*;
ImportJavax. servlet. http .*;
ImportCom. lowagie. Text .*;
ImportCom.lowagie.text= .*;
Public VoidDoget (httpservletrequest request,
Httpservletresponse response)
 ThrowsIoexception, servletexception
{
Document document =NewDocument (pagesize. A4, 36,36, 36,36 );
Bytearrayoutputstream BA =NewBytearrayoutputstream ();
Try
{
Author writer = author writer. getinstance (document, BA );
Document. open ();
Document. Add (NewParagraph ("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. I used the second method in the project. The source code of this article is successfully debugged on Tomcat 4. I hope this will bring convenience to you. Reprinted: Java Research Organization: moneyqb
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.