java导出pdf

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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值