利用iText在JSP中生成PDF報表

iText簡介

  iText是一個開放源碼的Java類庫,可以用來方便地生成PDF文件。大家通過訪問http://sourceforge.net/project/showfiles.php?group_id=15255&release_id=167948下載最新版本的類庫,下載完成之後會得到一個.jar包,把這個包加入JDK的classpath即可使用。如果生成的PDF文件中需要出現中文、日文、韓文字符,則還需要通過訪問http://itext.sourceforge.net/downloads/iTextAsian.jar下載iTextAsian.jar包。

  關於iText類庫的使用,http://www.lowagie.com/iText/tutorial/index.html有比較詳細的教程。該教程從入門開始,比較系統地介紹了在PDF文件中放入文字、圖片、表格等的方法和技巧。讀完這片教程,大致就可以做一些從簡單到複雜的PDF文件了。不過,試圖通過教程解決在生成PDF文件過程中遇到的所有困難無疑是一種奢望。所以,閱讀iText的api文檔顯得非常重要。讀者在下載類庫的同時,也可以下載類庫的文檔。

  如何利用iText在java程序中生成PDF報表

  以下是上述教程中一個最簡單的例子,這個例子刻畫了通過iText生成PDF文件的一般程序框架。讀者只需要在document.open();和document.close();兩條語句中間加入自己希望放在PDF文件中的內容即可。該例子只在PDF文件中加了“Hello World“一行文字。
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();

  由以上的例子可見,程序的框架十分清楚明瞭。然而在PDF中指定文字、圖畫、表格的位置是一件非常麻煩的事情。除了不斷地在程序中修改位置、然後運行程序、生成PDF文件、觀察元素在PDF中的位置是否合理這樣的過程以外,似乎還沒有其它更好的方法。
如何通過JSP生成PDF報表

  這一部分是在iText的教程中所沒有的,網上的相關資料也比較少。我曾在CSDN上看過有人開帖詢問實現細節,有人回覆了實現的原理:先在服務器上生成PDF文件,然後用戶通過點擊指向PDF文件的超鏈接選擇下載或打開。這是一個思路,或者說是思路之一。本文實現了這個思路,又給出另外一個思路並通過兩種途徑實現之。

  1)直接在服務器上生成PDF文件。

<%@ 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){}%>

  上面的程序在服務器上生成了一個靜態的PDF文件。顯然,每次運行所得的PDF文件的名稱應該是獨一無二不能有重的。本程序通過隨機函數來命名生成的PDF文件。本程序的缺點就是,每次運行都會在服務器上產生一個PDF文件,如果不及時刪除,數量會越來越大,這顯然是站點維護者所不願意看到的。

  2)將PDF文件通過流的形式輸送到客戶端的緩存。這樣做的好處是不會在服務器上留下任何“遺蹟”。

  i)直接通過JSP頁面生成

<%@
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] );
 }
%>

  ii)通過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();
 }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章