1. Introduction to iText
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.
2. How to Use iText to generate a PDF report on the JSP page
The following is the simplest example. 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
- {
- 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();
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.
3. How to generate a PDF report through JSP
This part is not found in the iText tutorial, and there is little relevant information on the Internet. After a period of research, I found that a PDF file is generated on the server first, and then the user selects 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 report 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 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 ".
◆ Generate a PDF report directly on the 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 < bytes.length;
- i++ )
- {
- output.writeByte( bytes[i] );
- }
- %>
◆ Generate a PDF report using 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();
- }
- Get database connection in JSP
- Introduction to JSP Action
- Simplified code in JSP expressions
- Detailed explanation of JSP-to-Servlet Conversion
- Introduction to JSP Elements