开发者社区> 问答> 正文

Itext PDF 导出出错,说是反射问题,也报the document has?400报错

Itext PDF 导出出错,说是反射问题,也报the document has no pages,急求大虾帮忙看看!!谢谢!!? 400 报错

前台jsp 页面 通过js 调用 Action 里的方法.如下:

public void getPdfFileForDetail(HttpServletRequest request,
   HttpServletResponse response) throws RemoteException,Exception{
    response.setContentType("contentType=application/pdf");
  request.setCharacterEncoding("UTF-8");
  
  String method = HttpUtil.getParameter(request, "method");//PDF文件的操作方式:handLoad--手动下载到本地,ftp--写到ftp服务器上
  
  /** 设置PDF文档的样式 **/

  // 定义一个A4大小的矩形组件
  Rectangle rect = new Rectangle(PageSize.A4);
  // 设置背景颜色为浅灰色
  // rect.setBackgroundColor(BaseColor.LIGHT_GRAY);
  // 设置border类型为box
  rect.setBorder(rect.BOX);
  // 设置border的颜色为浅灰色
  rect.setBorderColor(BaseColor.LIGHT_GRAY);
  // 设置border的宽度
  rect.setBorderWidth(5);
  // 创建一个PDF文档,将rect作为文档的预设样式,后面的10,10,10,10是文档的边外距
  Document document = new Document(rect, 10, 10, 10, 10);
  // 页边空白
  document.setMargins(20, 20, 30, 40);

  ByteArrayOutputStream ba = new ByteArrayOutputStream();
  String filename = "";
  try {
         /****统计信息****/
   String ysCount = HttpUtil.getParameter(request, "ysCount");//预算工时
   String ysTotal = HttpUtil.getParameter(request, "ysTotal");//需求预算数
   String compGsCount = HttpUtil.getParameter(request, "compGsCount")+"人日";//完成工时数量
   String completeCount = HttpUtil.getParameter(request, "completeCount");//完成需求数量
   String modifyCount = HttpUtil.getParameter(request, "modifyCount");//需求变更数量
   String hjGsCount = HttpUtil.getParameter(request, "hjGsCount");//核减工时数
   String compProZb = HttpUtil.getParameter(request, "compProZb")+"%";//工时使用率
   String compBl = HttpUtil.getParameter(request, "compBl")+"%";//需求完成率
   String modifyB = HttpUtil.getParameter(request, "modifyB")+"%";//需求变更率
   String hjGsBl = HttpUtil.getParameter(request, "hjGsBl")+"%";//核减工时率
   String jsGsCount = HttpUtil.getParameter(request, "jsGsCount");//结算工时数量
   String conResXs = HttpUtil.getParameter(request, "conResXs");//合同结算系数


   filename = contract_name + "结算明细.pdf";// 文件名
   PdfWriter writer = PdfWriter.getInstance(document, ba);
   
   //****设置页眉页脚****/
   //**     code    **/
   //设置PDF字体
   BaseFont base = null;
   Font fontchinese = null;
   Font headFont1 = null;
   Font headFont2 = null;
   Font headFont3 = null;
   try {
             base = BaseFont.createFont(PdfTextExtractor.class.getResource("/") + "/simsun.ttc,1", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);

    fontchinese = new Font(base, 8, Font.NORMAL);
    headFont1 = new Font(base, 8, Font.BOLD);
    headFont2 = new Font(base, 14, Font.BOLD);
    headFont3 = new Font(base, 10, Font.BOLD);
   } catch (DocumentException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }
     document.open();
     Paragraph title = new Paragraph(contract_name
     + "结算明细", headFont2);
   title.setSpacingAfter(20f);// 设置文档题目下方的空白高度
   title.setAlignment(Element.ALIGN_CENTER);// 文档标题居中
   title.setLeading(1f);// 设置行间距,设置上面空白宽度
   document.add(title);// 标题

// 写入合同统计信息
   Chunk title2 = new Chunk("合同统计信息", headFont3);
   title2.setBackground(new BaseColor(0xEE,0xEE,0xEE),0,0,495,0);
//   title2.setLineHeight(5);
   Paragraph titleTwo = new Paragraph();
   titleTwo.add(title2);
   document.add(titleTwo);
   float[] tb2_widths = {100f,100f,100f,100f};
   PdfPTable table2 = new PdfPTable(tb2_widths);
   table2.setSpacingBefore(5f);// 设置表格上面空白宽度
   table2.setSpacingAfter(10f);// 设置表格下面空白宽度
   table2.setTotalWidth(400);// 设置表格的宽度
   table2.setWidthPercentage(100);// 设置表格宽度为%100
   table2.setLockedWidth(false);// 设置表格的宽度不固定
   table2.getDefaultCell().setBorder(0);// 设置表格默认为无边框
//   table2.getDefaultCell().setBorderColor(BaseColor.WHITE);
   PdfPCell countCell = null;
   for (int n = 1; n <= 12; n++) {
    if (n == 1) {
     countCell = new PdfPCell(new Paragraph("预算工时:"
       + ysCount, fontchinese));
    } else if (n == 2) {
     countCell = new PdfPCell(new Paragraph("需求预算数:"
       + ysTotal, fontchinese));
    } else if (n == 3) {
     countCell = new PdfPCell(new Paragraph("完成工时数量:"
       + compGsCount, fontchinese));
    } else if (n == 4) {
     countCell = new PdfPCell(new Paragraph("完成需求数量:"
       + completeCount, fontchinese));
    } else if (n == 5) {
     countCell = new PdfPCell(new Paragraph("需求变更数量:"
       + modifyCount, fontchinese));
    } else if (n == 6) {
     countCell = new PdfPCell(new Paragraph("核减工时数:"
       + hjGsCount, fontchinese));
    } else if (n == 7) {
     countCell = new PdfPCell(new Paragraph("工时使用率:"
       + compProZb, fontchinese));
    } else if (n == 8) {
     countCell = new PdfPCell(new Paragraph("需求完成率:"
       + compBl, fontchinese));
    } else if (n == 9) {
     countCell = new PdfPCell(new Paragraph("需求变更率:"
       + modifyB, fontchinese));
    } else if (n == 10) {
     countCell = new PdfPCell(new Paragraph("核减工时率:"
       + hjGsBl, fontchinese));
    } else if (n == 11) {
     countCell = new PdfPCell(new Paragraph("结算工时数量:"
       + jsGsCount, fontchinese));
    } else if (n == 12) {
     countCell = new PdfPCell(new Paragraph("合同结算系数:"
       + conResXs, fontchinese));
    }
    countCell.setFixedHeight(20);
    if(n==2 || n==12){
     countCell.setColspan(3);//合并单元格(列)
    }else{
     countCell.setColspan(1);//合并单元格(列)
    }
    countCell.setBorder(Rectangle.NO_BORDER);//设置表格无边框
    countCell.setHorizontalAlignment(Element.ALIGN_LEFT);// 设置内容水平左对齐显示
    countCell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 设置垂直居中
    table2.addCell(countCell);
   }
   document.add(table2);
 } catch (FileNotFoundException e) {
   e.printStackTrace();
   log.error("生成PDF出错 FileNotFoundException: ", e);
  } catch (DocumentException e) {
   e.printStackTrace();
   log.error("生成PDF出错 DocumentException", e);
  } finally {
   document.close();
   
       response.setContentType("contentType=application/pdf");
    response.setHeader("Content-Disposition", "attachment;filename=\""
      + StringUtil.toUTF8(filename) + "\"");
    ba.writeTo(response.getOutputStream());
    response.flushBuffer();
    System.out.println("PDF文件下载到本地!");
      }
  
 }

 报的错:

java.lang.reflect.InvocationTargetException
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:615)
 at com.ai.appframe2.web.action.RequestProcessor.exeMethod(RequestProcessor.java:104)
 at com.ai.appframe2.web.action.RequestProcessor.process(RequestProcessor.java:58)
 at com.ai.appframe2.web.action.CentralControlServlet.doGet(CentralControlServlet.java:60)
 at com.ai.appframe2.web.action.CentralControlServlet.doPost(CentralControlServlet.java:70)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
 at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:966)
 at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:907)
 at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:145)
 at com.ai.appframe2.web.filter.expire.ExpireResponseHeaderFilter.doFilter(ExpireResponseHeaderFilter.java:135)
 at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:190)
 at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:130)
 at com.ai.appframe2.web.filter.LoginFilter.doFilter(LoginFilter.java:176)
 at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:190)
 at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:130)
 at com.ai.appframe2.monitor.URLFilter.doFilter(URLFilter.java:62)
 at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:190)
 at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:130)
 at com.ibm.ws.webcontainer.filter.WebAppFilterChain._doFilter(WebAppFilterChain.java:87)
 at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:696)
 at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:641)
 at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:475)
 at com.ibm.ws.wswebcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:463)
 at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:92)
 at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:744)
 at com.ibm.ws.wswebcontainer.WebContainer.handleRequest(WebContainer.java:1425)
 at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:92)
 at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:465)
 at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:394)
 at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:102)
 at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:152)
 at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:213)
 at com.ibm.io.async.AbstractAsyncFuture.fireCompletionActions(AbstractAsyncFuture.java:195)
 at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:136)
 at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:193)
 at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:725)
 at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:847)
 at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1498)
Caused by:
ExceptionConverter: java.io.IOException: The document has no pages.

用的itext jar包为:itextpdf-5.3.5.jar,itext-asian.jar

急求各位大侠 帮忙解决,感激不尽!!

展开
收起
爱吃鱼的程序员 2020-06-04 16:30:17 1204 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    没人回答啊 ,,,是我描述的不清楚吗? 我刚开始学,,,请理解一哈.######这么长一坨代码,怎么看?建议你先做个简单的demo.要先能导出PDF.然后再添加数据,一步一步来,至少知道问题在哪里######本地环境能够导出PDF ,就是发布到服务器上就报错 我也是这样的 楼主怎么解决的?######3Q. 忘记说了 本地环境能够导出PDF ,就是发布到服务器上就报错######

    一般这个问题 内容行是kong'de

    2020-06-04 17:10:41
    赞同 展开评论 打赏
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
对象存储实战指南-试读 立即下载
OpenAnolis 龙蜥操作系统开源社区技术创新白皮书 立即下载
使用CNFS搭建弹性Web服务 立即下载