选择显示字体大小

利用itext在jsp中生成pdf报表

1、概述
  企业的一些信息通过网络形成html报表,虽然ie可以直接打印显示在其中的内容,但是从界面上来看,如果直接将html的显示结果打印出来,显得不太美观。如果将它转成pdf文件再打印,则打印效果会好很多。

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

3、如何利用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中的位置是否合理这样的过程以外,似乎还没有其它更好的方法。

4、如何通过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文件通过流的形式输送到客户端的缓存。这样做的好处是不会在服务器上留下任何“遗迹”

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



5、结束

  我采用的是第二种方法。本文的源码在tomcat上面都是调试通过的。希望可以给大家带来方便。

  


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons