title: Send a PDF to browser via JSF
body: <p>I am trying to send a JasperReports generated PDF file to user's browser, I can't find what's wrong in my managed bean method, here's a snippet:</p>
  
  <pre><code>System.out.println("Making pdf...");
  
          FacesContext fc = FacesContext.getCurrentInstance();
          ExternalContext ec = fc.getExternalContext();
          String tplPath = ec.getRealPath("testTemplate.jrxml");
          JasperReport jasperReport = JasperCompileManager.compileReport(tplPath);
          JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap&lt;String,Object&gt;(), ds );
  
          String pdfName = "/testReport.pdf";
          String pdfPath = ec.getRealPath(pdfName);
          JasperExportManager.exportReportToPdfFile(jasperPrint, pdfPath);
  
          System.out.println("PDF ready!");
  
          ec.responseReset(); 
          ec.setResponseContentType(ec.getMimeType(pdfPath)); 
          //ec.setResponseContentLength(contentLength); 
          ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + pdfName + "\""); 
  
          InputStream input = new FileInputStream(pdfPath);
          OutputStream output = ec.getResponseOutputStream();
          IOUtils.copy(input, output);
  
          System.out.println("Sending to browser...");
  
          fc.responseComplete();  
  </code></pre>
  
  <p>It's called by a simple user click:</p>
  
  <pre><code>&lt;p:menuitem value="TestPDF" action="#{menuController.getTestPdf()}" /&gt;
  </code></pre>
  
  <p>I feel like it's something easy to find out. Why can't I see it? :)</p>