4 Replies Latest reply on Aug 1, 2007 9:46 AM by Pete Muir

    Open PDF in new window

    jfrankman Novice

      I am trying to stream a PDF to a new browser window. I can get this to work, but the strange thing that happens is that a new browser window gets opened, but instead of the PDF geting displayed inside the browser window, it is opened separately by Adobe Acrobat. So, the user is forced to close two windows, the Adobe Acrobat window and the blank Browser window. I have gone over my code but have never seen any behavior quite like this.

      What will cause a new browser window to open up, but still open Adobe Acrobat outside of the browser?

      Any thoughts on why this is happening?

      Could this have something to do with using the seam link or is this more likely a JSF response issue?

      My code is as follows:

      JBoss Seam Link:

      <s:link id="link1"
       action="#{reportDocumentBean.viewPhysicalDamagePDF}" target="_blank">
       <h:outputText value="Print" />
       </s:link>


      here is the heart of the Print Report Bean's method:

       HttpServletResponse response = (HttpServletResponse) faces
       .getExternalContext().getResponse();
       response.setContentType("application/pdf");
       response.setContentLength(pdf.length);
       response.setHeader("Content-disposition", "attachment;filename=\""
       + reportName+"crystalreport.pdf" + "\"");
      
       ServletOutputStream out;
       out = response.getOutputStream();
       out.write(pdf);
       out.flush(); //new
      
       out.close();
       bais.close();
       reportClientDocument.close();
       faces.responseComplete(); //new
      
      

      Here is the entire method incase you need to see the context of the snippet above.

       private void viewPDF(String reportName, String fullClassName, String tableAlias, String subreportTableAlias, List dataSet)
       {
       FacesContext faces = FacesContext.getCurrentInstance();
      
       try {
       ReportClientDocument reportClientDocument = new ReportClientDocument();
       String report = reportName;
       //String report = "RDC_To_CR.Net.rpt";
       reportClientDocument.open(report, 0);
      
      
       List<CrystalDTO> crystalTranferObjects = new ArrayList<CrystalDTO>();
      
       //fill dto
       CrystalDTO crystalDTO1 = new CrystalDTO(dataSet,
       fullClassName,
       tableAlias, subreportTableAlias);
       crystalTranferObjects.add(crystalDTO1);
      
      
      
       Iterator crystalDTOIterator= crystalTranferObjects.iterator();
       while(crystalDTOIterator.hasNext())
       {
       CrystalDTO dto=(CrystalDTO)crystalDTOIterator.next();
      
       ReportCreator.passPOJO(reportClientDocument, dto.getDataSet(), dto.getClassName(), dto.getTableAlias(), dto.getSubreportName());
       }
      
       ByteArrayInputStream bais;
       bais = (ByteArrayInputStream) reportClientDocument
       .getPrintOutputController().export(ReportExportFormat.PDF);
       byte[] pdf = new byte[bais.available()];
       bais.read(pdf, 0, bais.available());
      
       HttpServletResponse response = (HttpServletResponse) faces
       .getExternalContext().getResponse();
       response.setContentType("application/pdf");
       response.setContentLength(pdf.length);
       response.setHeader("Content-disposition", "attachment;filename=\""
       + reportName+"crystalreport.pdf" + "\"");
       ServletOutputStream out;
       out = response.getOutputStream();
       out.write(pdf);
       out.flush(); //new
      
       out.close();
       bais.close();
       reportClientDocument.close();
       faces.responseComplete(); //new
      
       }
      
       catch (ReportSDKException e) {
       e.printStackTrace();
       } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       } catch (ClassNotFoundException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       }
       faces.responseComplete();
       }