您的位置:首页 > Web前端 > JavaScript

使用JSP页面生成PDF报表

2015-11-10 17:14 696 查看
转载自:http://developer.51cto.com/art/200907/134261.htm

1、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文档显得非常重要。读者在下载类库的同时,也可以下载类库的文档。

2、如何利用iText在JSP页面中生成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的教程中所没有的,网上的相关资料也比较少。我经过一段时间研究发现:先在服务器上生成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文件通过流的形式输送到客户端的缓存。这样做的好处是不会在服务器上留下任何“遗迹”。

◆直接通过JSP页面生成PDF报表

<%@  
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;  
< bytes.length;  
i++ )  
{  
output.writeByte( bytes[i] );  
}  
%> 

◆通过Servlet生成PDF报表
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();  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: