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

    Open PDF in new window

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


        • 1. Re: Open PDF in new window
          damianharvey

          That's normally a browser issue in the way that it handles PDFs. Was this Firefox?

          You could always put a hidden iframe on your page and set the target of your s:link to that.

          Cheers,

          Damian.

          • 2. Re: Open PDF in new window
            pmuir

            Set the content disposition to inline.

            • 3. Re: Open PDF in new window

              Thanks for your replies.

              The browser is IE. When I test it with firefox everything works fine. I have changed the content disposition from atachment to inline, but I still get the pdf being displayed in a new window. I don't think this is just a configuration matter with IE because if I open a PDF on other websites, the PDF opens correctly inside of IE. One thing I can think is that something is corrupt in the http header. I tried to capture it using the Eclipse TCP/IP monitor, but it did not work with SEAM. Are there any IE or firefox plugin that logs HTTP responses?

              • 4. Re: Open PDF in new window
                pmuir

                It's just the way IE interprets http headers vs. firefox. There are lots of examples out there of the correct header set to use (e.g. in the php docs). Firebug will give you http headers.