Origin
Not long ago, I made a small project to generate PDF reports through JSP, opening my eyes. 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.
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.
How to Use itext in JavaProgramGenerate a PDF report
The following is the simplest example in the above tutorial. This example depicts the general program framework for generating PDF files through itext. 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 = new document ();
Try
{
Using writer. getinstance (document, new fileoutputstream ("chap0101.pdf "));
Document. open ();
Document. Add (new paragraph ("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.
How to generate a PDF report using JSP
This part is not found in the itext tutorial, and there is little relevant information on the Internet. 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.
<% @ Page import = "com. lowagie. Text. *, com.lowagie.text=. *, java. Io. *" %>
<%
String filename = "pdf" + (new random (). nextint () + ". pdf ";
Document document = new document (pagesize. A4 );
Servletoutputstream out1 = response. getoutputstream ();
Try
{
Using writer = Using writer. 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 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.
<% @
Page import = "Java. Io. *, java. AWT. Color, Com. lowagie. Text. *, com.lowagie.text}. *" %>
<%
Response. setcontenttype ("application/pdf ");
Document document = new document ();
Bytearrayoutputstream buffer = new bytearrayoutputstream ();
Response writer = Response writer. 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 <bytes. length; 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= .*;
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
{
Author writer = author writer. getinstance (document, BA );
Document. open ();
Document. Add (new paragraph ("Hello World "));
}
Catch (incluentexception 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 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.
You are welcome to use it. If you need to reprint it, please indicate the source.