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

前端 未结 3 1776
死守一世寂寞
死守一世寂寞 2020-12-01 11:50

I\'m trying to show inline PDF which is opened in new browser window. I have following scenario:

  1. In some ActionListen which is called by ajax I generate PDF
3条回答
  •  庸人自扰
    2020-12-01 11:59

    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:

    
    

    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.

提交回复
热议问题