1

I'm trying to send a PDF to browser throught a JSF response, a very simple document, see:

public void gerarPDF() throws IOException{
     try {
         HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();

            // step 1
            Document document = new Document();
            // step 2
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PdfWriter.getInstance(document, baos);
            // step 3
            document.open();
            // step 4
            document.add(new Paragraph("HELLO WORLD"));
            document.add(new Paragraph("ANOTHER LINE"));
            // step 5
            document.close();

            response.addHeader("Content-Type", "application/force-download");
            response.addHeader("Content-Disposition", "attachment; filename=report.pdf");
            response.setContentLength(baos.size());
            response.getOutputStream().write(baos.toByteArray());
            response.getOutputStream().flush();

            baos.flush();
            baos.close();
            FacesContext.getCurrentInstance().responseComplete();
        }
        catch(DocumentException e) {
            throw new IOException(e.getMessage());
        }
}

This method is called from a commandButton, the problem browser shows that:

%PDF-1.4 %2 0 obj <>stream x+S00SI

If i change my code to save in a file, everything works fine, see:

FileOutputStream fos = new FileOutputStream("C:\\"+UUID.randomUUID().toString()+".pdf");
        fos.write(baos.toByteArray());
        fos.close();

But i need show the PDF in browser. Someone can helpe me ?

Ronaldo Lanhellas
  • 2,975
  • 5
  • 46
  • 92
  • 1
    Use a plain servlet for downloading, as in [Implementing a simple file download servlet](https://stackoverflow.com/questions/1442893). JSF is not the right tool for this. – f_puras Nov 09 '15 at 15:49
  • BUt how can i send information from ManagedBean to Servlet ? – Ronaldo Lanhellas Nov 09 '15 at 16:06
  • `response.setContentType("application/pdf");response.add(set)Header("Content-Disposition", "attachment; filename=\"report.pdf\"");` – Tiny Nov 09 '15 at 17:23
  • i already tried this and didn't work. – Ronaldo Lanhellas Nov 09 '15 at 17:26
  • Which browser are you trying with? Does that browser support a PDF reader? Is the one already installed on the browser? – Tiny Nov 09 '15 at 17:31
  • @Scientist See [Get JSF managed bean by name in any Servlet related class](https://stackoverflow.com/questions/2633112) – f_puras Nov 09 '15 at 17:33
  • I'm trying in Internet Explorer 8 and Google Chrome. Is just in my application, i tried this and works normally (http://itextpdf.com:8180/book/) – Ronaldo Lanhellas Nov 09 '15 at 17:34
  • 1
    @f_puras : Downloading a file is only the matter of setting an appropriate content type and a few other response headers. Using a Servlet in place of a managed bean will not make an essential difference. – Tiny Nov 09 '15 at 17:56
  • @Tiny All right, why not show us your solution using a managed bean? – f_puras Nov 09 '15 at 18:07
  • 1
    @Tiny Ok, I see: [How to provide a file download from a JSF backing bean?](https://stackoverflow.com/questions/9391838). You're right! – f_puras Nov 09 '15 at 18:09
  • THe problem was solved, thank you Tiny. I removed the "ajax" from my commandButton and everything works. – Ronaldo Lanhellas Nov 09 '15 at 18:49

1 Answers1

2

Try changing the content type to application/pdf:

response.addHeader("Content-Type", "application/pdf");

Ref. Proper MIME media type for PDF files.

f_puras
  • 2,521
  • 4
  • 33
  • 38