2 Replies Latest reply on Mar 11, 2011 5:58 AM by houda

    download a PDF rather than opening it

    mikebgx

      Hi Folks,


      My app produces a PDF report which I can show to the user by the code below, it opens in Preview ie the app that is registered to handle PDF files.  But I'd also like to allow the user to just save it to disk without viewing it, any ideas please?


      Regards,


      Mike Burton


      byte[] pdf= rpt.toPdf();        // Produce PDF into our byte array
      // Now render PDF bytes to browser...
      HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
      OutputStream os = response.getOutputStream();
      response.setContentType("application/pdf");
      response.setContentLength(pdf.length);
      response.setHeader("Content-disposition", "attachment; filename=\""+ rpt.getOutName()+ ".pdf\"");
      os.write(pdf);
      os.flush();
      os.close();
      facesContext.responseComplete();

        • 1. Re: download a PDF rather than opening it
          mikebgx

          Sorry, code didnt format well, should read:




          byte[] pdf= rpt.toPdf();        // Produce PDF into our byte array
          // Now render PDF bytes to browser...
          HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
          OutputStream os = response.getOutputStream();
          response.setContentType("application/pdf");
          response.setContentLength(pdf.length);
          response.setHeader("Content-disposition", "attachment; filename=\""+ rpt.getOutName()+ ".pdf\"");
          os.write(pdf);
          os.flush();
          os.close();
          facesContext.responseComplete();



          • 2. Re: download a PDF rather than opening it
            houda

            Hi , would please write all the code .