服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

详解如何利用itext在jsp中生成pdf报表


  前久做了一个通过jsp生成pdf报表的小项目,算得上开了一次眼界。企业的一些信息通过网络形成html报表,虽然ie可以直接打印显示在其中的内容,但是从界面上来看,如果直接将html的显示结果打印出来,显得不太美观。如果将它转成pdf文件再打印,则打印效果会好很多。
  
  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在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中的位置是否合理这样的过程以外,似乎还没有其它更好的方法。
  
  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文件通过流的形式输送到客户端的缓存。这样做的好处是不会在服务器上留下任何“遗迹”。
  
  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();
  }
  
  4、结束
  
  我在项目中采用的是第二种方法。本文的源码在我的tomcat4上面都是调试通过的。希望可以给大家带来方便。

扫描关注微信公众号