Unable to show PDF in p:media generated from streamed content in Primefaces

I can reproduce your problem. It indeed doesn’t work in Firefox (nor in IE9, but it works in Chrome). PrimeFaces lead Cagatay has also mentioned that several times.

I’m not sure if this is a bug in the PrimeFaces resource handler or in the browser. I’ll leave it in the middle.

In the meanwhile, your best bet is a simple web servlet for the job. Just create this class:

@WebServlet("/report.pdf")
public class PdfReportServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        byte[] content = (byte[]) request.getSession().getAttribute("reportBytes");
        response.setContentType("application/pdf");
        response.setContentLength(content.length);
        response.getOutputStream().write(content);
    }

}

And invoke it as follows:

<p:media value="/report.pdf" ... />

That’s it. No XML config necessary. It works for me in all browsers. Depending on the functional requirements, you may want to further finetune response headers related to browser caching.

Leave a Comment