詳解如何利用iText在JSP中生成PDF報表
2024-09-05 00:20:09
供稿:網(wǎng)友
前久做了一個通過jsp生成pdf報表的小項目,算得上開了一次眼界。企業(yè)的一些信息通過網(wǎng)絡形成html報表,雖然ie可以直接打印顯示在其中的內容,但是從界面上來看,如果直接將html的顯示結果打印出來,顯得不太美觀。如果將它轉成pdf文件再打印,則打印效果會好很多。
1、itext簡介
itext是一個開放源碼的java類庫,可以用來方便地生成pdf文件。大家通過訪問http://sourceforge.net/project/showfiles.php?group_id=15255&release_id=167948下載最新版本的類庫,下載完成之后會得到一個.jar包,把這個包加入jdk的classpath即可使用。
如果生成的pdf文件中需要出現(xiàn)中文、日文、韓文字符,則還需要通過訪問http://itext.sourceforge.net/downloads/itextasian.jar下載itextasian.jar包。
關于itext類庫的使用,http://www.lowagie.com/itext/tutorial/index.html有比較詳細的教程。該教程從入門開始,比較系統(tǒng)地介紹了在pdf文件中放入文字、圖片、表格等的方法和技巧。
讀完這片教程,大致就可以做一些從簡單到復雜的pdf文件了。不過,試圖通過教程解決在生成pdf文件過程中遇到的所有困難無疑是一種奢望。所以,閱讀itext的api文檔顯得非常重要。讀者在下載類庫的同時,也可以下載類庫的文檔。
2、如何利用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中的位置是否合理這樣的過程以外,似乎還沒有其它更好的方法。
3、如何通過jsp生成pdf報表
這一部分是在itext的教程中所沒有的,網(wǎng)上的相關資料也比較少。我經(jīng)過一段時間研究發(fā)現(xiàn):先在服務器上生成pdf文件,然后用戶通過點擊指向pdf文件的超鏈接選擇下載或打開。這是一個思路,或者說是思路之一。本文實現(xiàn)了這個思路,又給出另外一個思路并通過兩種途徑實現(xiàn)之。
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){}
%>
上面的程序在服務器上生成了一個靜態(tài)的pdf文件。顯然,每次運行所得的pdf文件的名稱應該是獨一無二不能有重的。本程序通過隨機函數(shù)來命名生成的pdf文件。本程序的缺點就是,每次運行都會在服務器上產(chǎn)生一個pdf文件,如果不及時刪除,數(shù)量會越來越大,這顯然是站點維護者所不愿意看到的。
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();
}
4、結束
我在項目中采用的是第二種方法。本文的源碼在我的tomcat4上面都是調試通過的。希望可以給大家?guī)矸奖恪?br>