`

Itext笔记3

阅读更多

前久做了一个通过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上面都是调试通过的。希望可以给大家带来方便。

本文来自:http://doc.linuxpk.com/45283.html

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics