这一部分是在iText的教程中所没有的,网上的相关资料也比较少。我曾在CSDN上看过有人开帖询问实现细节,有人回复了实现的原理:先在服务器上生成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();
  }

  结束

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

  1065:无效的SQL语句,SQL语句为空

 

  1081:不能建立Socket连接

  1114:数据表已满,不能容纳任何记录

  1116:打开的数据表太多

  1129:数据库出现异常,请重启数据库

  1130:连接数据库失败,没有连接数据库的权限

  1133:数据库用户不存在

  1141:当前用户无权访问数据库

  1142:当前用户无权访问数据表

  1143:当前用户无权访问数据表中的字段

  1146:数据表不存在

  1147:未定义用户对数据表的访问权限

  1149:SQL语句语法错误

  1158:网络错误,出现读错误,请检查网络连接状况

  1159:网络错误,读超时,请检查网络连接状况

  1160:网络错误,出现写错误,请检查网络连接状况

  1161:网络错误,写超时,请检查网络连接状况

  1062:字段值重复,入库失败

  1169:字段值重复,更新记录失败

  1177:打开数据表失败

  1180:提交事务失败

  1181:回滚事务失败

  1203:当前用户和数据库建立的连接已到达数据库的最大连接数,请增大可用的数据库连接数或重启数据库

  1205:加锁超时

  1211:当前用户没有创建用户的权限

  1216:外键约束检查失败,更新子表记录失败

  1217:外键约束检查失败,删除或修改主表记录失败

  1226:当前用户使用的资源已超过所允许的资源,请重启数据库或重启服务器

       1227:权限不足,您无权进行此操作

  1235:MySQL版本过低,不具有本功能