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