0 Replies Latest reply on Apr 18, 2008 2:31 PM by Nagarajan Sivakumar

    Dynamic generation of PDFs in JSF

    Nagarajan Sivakumar Newbie

      Hello,
      I have a question about dynamically generating PDF documents within JSF. I am using iText Java PDF libraries to generate the PDF in a separate servlet - how ever when its time to display the bytes written to the servlet output stream, IE7 does not show up any thing at all.

      I was asked to add a FacesContext.responseComplete statement because i was trying to render a non JSF component page. But that does not seem to help.

      When i try the same application on FireFox, the binary contents of the PDF get displayed - even though i am setting the content type to application/pdf on the response object.

      Here's the servlet code

      public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
      log.info("Inside doPost....");
      viewToFromReportPDF(request, response);
      }


      public void viewToFromReportPDF(HttpServletRequest request, HttpServletResponse response) throws IOException{
      log.info("Inside PDFUtilServlet.......");
      String reportID = (String)request.getParameter("reportID");
      log.info("ReportID ==>"+reportID);
      response = configureResponse(response);
      Document document = new Document();
      ByteArrayOutputStream baos = new ByteArrayOutputStream();

      try{
      PdfWriter writer = PdfWriter.getInstance(document, baos);
      log.info("Abt to create PDF of report ID "+reportID);
      document.open();
      document.add(new Paragraph(reportID));
      document.close();
      }catch(DocumentException de) {
      log.debug(de.getMessage());
      }
      response.setContentLength(baos.size());
      ServletOutputStream out = response.getOutputStream();
      out.write(baos.toByteArray());
      }


      private HttpServletResponse configureResponse(HttpServletResponse response) {

      response.setHeader("Expires", "0");
      response.setHeader("Cache-Control", "max-age=60");
      response.setHeader("Accept-Ranges", "bytes");
      response.setHeader("Connection","close");
      response.setContentType("application/pdf");
      return response;
      }


      Is there some thing that i am missing here ? I am using IE7 and JSF RichFaces 3.1.4 running against Tomcat 6.0.16

      Thanks,
      NS