java导出pdf

来源:互联网 发布:模型预测控制 知乎 编辑:程序博客网 时间:2024/05/16 18:38
所需jar:

itext-1.00.jar

iTextAsian.jar

 

List authlist = queryList();//确定List集合的数据
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  try {

   List totalList = new ArrayList();
  
   // 创建一个Document对象
   com.lowagie.text.Document document = new com.lowagie.text.Document(PageSize.A4, 36, 36, 36, 36);
   ByteArrayOutputStream ba = new ByteArrayOutputStream();
   //设置宋体
   BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
      "UniGB-UCS2-H", true);
   logger.info("test");
   
   logger.info("test1");
   Font fontChinese =new Font(bfChinese,10,Font.NORMAL,Color.GREEN);
   Font normalfont =new Font(bfChinese,9,Font.NORMAL,Color.BLACK);
   logger.info("sss");
   try {
    //添加一些信息
    document.addTitle("历史交易记录查询结果");

    PdfWriter writer = PdfWriter.getInstance(document, ba);
    document.open();
    document.add(new Paragraph("交易验证总笔数:"+String.valueOf(authlist.size())+"\t成功笔数:"+String.valueOf(request.getAttribute("success"))+"\t失败笔数:"+String.valueOf( request.getAttribute("failure")),fontChinese));
    document.add(new Paragraph("\r\n\t\t\t\t交易时间\t\t\t\t\t\t交易种类\t\t\t\t交易号\t\t\t\t商户代码\t\t\t\t\tVCN虚拟卡号\t\t\t\t交易结果",fontChinese));
    //填充数据
    for (int i = 0; i < authlist.size(); i++) {// 行
     AuthTransactionMessageData auth = (AuthTransactionMessageData) authlist
       .get(i);
     int tmpresult = auth.getResult();
     String result = "";
     if(tmpresult==4){
      result="成功";
     }
     else{
      result="失败";
     }
    document.add(new Paragraph(String.valueOf(auth.getAuthTime())+"\t\t\t\t\t支付验证\t\t\t\t"+auth.getTradeRecordID()+"\t\t\t\t\t"+auth.getMerID()+"\t\t\t\t\t"+auth.getPan()+"\t\t\t\t\t"+result,normalfont));
    }
   } catch (DocumentException de) {
    de.printStackTrace();
    logger.info("A Document error:" + de.getMessage());
   }
   document.close();
   String filename2 = "历史交易记录查询结果"+(new SimpleDateFormat("yyyy-MM-dd")).format(new Date()) + ".pdf";
   response.setHeader("Content-disposition", "attachment; filename="
     + new String(filename2.getBytes("gb2312"), "iso8859-1"));
   response.setContentType("application/pdf");
   response.setContentLength(ba.size());
   ServletOutputStream out = response.getOutputStream();
   ba.writeTo(out);
   out.flush();
   out.close();
  } catch (Exception e) {
   logger.info("error");
   e.printStackTrace();
  }

 

原创粉丝点击