使用JSP頁(yè)面生成PDF報(bào)表
1、iText簡(jiǎn)介
iText是一個(gè)開(kāi)放源碼的Java類庫(kù),可以用來(lái)方便地生成PDF文件。大家通過(guò)訪問(wèn)http://sourceforge.net/project/showfiles.php?group_id=15255&release_id=167948下載***版本的類庫(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類庫(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文檔顯得非常重要。讀者在下載類庫(kù)的同時(shí),也可以下載類庫(kù)的文檔。
2、如何利用iText在JSP頁(yè)面中生成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文件,然后用戶通過(guò)點(diǎn)擊指向PDF文件的超鏈接選擇下載或打開(kāi)。這是一個(gè)思路,或者說(shuō)是思路之一。本文實(shí)現(xiàn)了這個(gè)思路,又給出另外一個(gè)思路并通過(guò)兩種途徑實(shí)現(xiàn)之。
1)直接在服務(wù)器上生成PDF報(bào)表。
- <%@ 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文件的名稱應(yīng)該是***不能有重的。本程序通過(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ò)流的形式輸送到客戶端的緩存。這樣做的好處是不會(huì)在服務(wù)器上留下任何“遺跡”。
◆直接通過(guò)JSP頁(yè)面生成PDF報(bào)表
- <%@
- 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] );
- }
- %>
◆通過(guò)Servlet生成PDF報(bào)表
- 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();
- }
【編輯推薦】