自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用JSP頁(yè)面生成PDF報(bào)表

開(kāi)發(fā) 后端
本文介紹使用JSP頁(yè)面生成PDF報(bào)表,介紹一個(gè)最簡(jiǎn)單的例子,這個(gè)例子刻畫(huà)了通過(guò)iText生成PDF文件的一般程序框架。

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“一行文字。

  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(); 

由以上的例子可見(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)表。

  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. %> 

上面的程序在服務(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)表

  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. %> 

◆通過(guò)Servlet生成PDF報(bào)表

  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. 在JSP中獲取數(shù)據(jù)庫(kù)連接
  2. 介紹JSP Action的使用
  3. 簡(jiǎn)化JSP表達(dá)式中代碼
  4. 詳解JSP向Servlet轉(zhuǎn)換
  5. 簡(jiǎn)單介紹JSP元素教程
責(zé)任編輯:佚名 來(lái)源: IT168
相關(guān)推薦

2009-07-02 13:12:33

JSP打印報(bào)表

2009-05-21 10:08:49

SQL報(bào)表JSPHibernate

2012-06-13 02:10:46

Java并發(fā)

2012-07-19 10:59:18

Jav并發(fā)

2009-03-16 15:07:20

JSP分頁(yè)window.openJSP表單

2009-08-14 13:37:25

ASP.NET靜態(tài)頁(yè)面

2009-02-25 10:55:29

FCKeditor控件JSP

2023-02-26 10:16:19

JavaPDF文檔

2011-12-01 14:14:51

Google

2009-07-02 13:18:53

JSP打印報(bào)表

2009-07-06 09:34:19

JSP頁(yè)面

2009-07-01 18:50:29

Dreamweaver

2009-07-03 18:12:49

JSP頁(yè)面

2013-04-07 10:42:56

Asp.Net頁(yè)面周期

2024-07-15 15:05:20

Python數(shù)據(jù)驅(qū)動(dòng)

2009-08-04 16:50:15

ASP.NET頁(yè)面生命

2009-08-04 16:05:15

ASP.NET頁(yè)面生命

2009-07-31 10:47:18

ASP.NET頁(yè)面生命

2009-07-06 10:00:31

JSP頁(yè)面?zhèn)髦?/a>

2009-06-10 17:03:36

JSP動(dòng)態(tài)生成
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)