Use JSP pages to generate PDF reports

Source: Internet
Author: User

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.

 
 
  1. Document document = new Document();  
  2. try  
  3. {  
  4. PdfWriter.getInstance  
  5. (document, new FileOutputStream  
  6. ("Chap0101.pdf"));  
  7. document.open();  
  8. document.add(new Paragraph("Hello World"));  
  9. }  
  10. catch(DocumentException de)  
  11. {  
  12. System.err.println(de.getMessage());  
  13. }  
  14. catch(IOException ioe)  
  15. {  
  16. System.err.println(ioe.getMessage());  
  17. }  
  18. 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.

 
 
  1. <%@ page import ="com.lowagie.text.*  
  2. ,com.lowagie.text.pdf.*, java.io.*"%> 
  3. <%  
  4. String filename =  
  5. "PDF"+(new Random()).nextInt()+".pdf" ;  
  6. Document document =  
  7. new Document(PageSize.A4);  
  8. ServletOutputStream out1 
  9. = response.getOutputStream();  
  10. try{  
  11. PdfWriter writer =  
  12. PdfWriter.getInstance(document,  
  13. new FileOutputStream(filename) );  
  14. document.open();  
  15. document.add(new Paragraph("Hello World"));  
  16. document.close();  
  17. }  
  18. catch(Exception e){}  
  19. %> 

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

 
 
  1. <%@  
  2. page import="java.io.*,  
  3. java.awt.Color,com.lowagie.text.*,  
  4. com.lowagie.text.pdf.*"%> 
  5. <%  
  6. response.setContentType  
  7. ( "application/pdf" );  
  8. Document document = new Document();  
  9. ByteArrayOutputStream buffer 
  10. = new ByteArrayOutputStream();  
  11. PdfWriter writer=  
  12. PdfWriter.getInstance( document, buffer );  
  13. document.open();  
  14. document.add(new Paragraph("Hello World"));  
  15. document.close();  
  16. DataOutput output =  
  17. new DataOutputStream  
  18. ( response.getOutputStream() );  
  19. byte[] bytes = buffer.toByteArray();  
  20. response.setContentLength(bytes.length);  
  21. for( int i = 0;  
  22. < bytes.length;  
  23. i++ )  
  24. {  
  25. output.writeByte( bytes[i] );  
  26. }  
  27. %> 

◆ Generate a PDF report using Servlet

 
 
  1. import java.io.*;  
  2. import javax.servlet.*;  
  3. import javax.servlet.http.*;  
  4. import com.lowagie.text.*;  
  5. import com.lowagie.text.pdf.*;  
  6. public void doGet  
  7. (HttpServletRequest request,  
  8. HttpServletResponse response)  
  9. throws IOException,ServletException  
  10. {  
  11. Document document =  
  12. new Document(PageSize.A4, 36,36,36,36);  
  13. ByteArrayOutputStream ba 
  14. = new ByteArrayOutputStream();  
  15. try  
  16. {  
  17. PdfWriter writer =  
  18. PdfWriter.getInstance(document, ba);  
  19. document.open();  
  20. document.add(new  
  21. Paragraph("Hello World"));  
  22. }  
  23. catch(DocumentException de)  
  24. {  
  25. de.printStackTrace();  
  26. System.err.println  
  27. ("A Document error:" +de.getMessage());  
  28. }  
  29. document.close();  
  30. response.setContentType  
  31. ("application/pdf");  
  32. response.setContentLength(ba.size());  
  33. ServletOutputStream out 
  34. = response.getOutputStream();  
  35. ba.writeTo(out);  
  36. out.flush();  
  1. Get database connection in JSP
  2. Introduction to JSP Action
  3. Simplified code in JSP expressions
  4. Detailed explanation of JSP-to-Servlet Conversion
  5. Introduction to JSP Elements

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.