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

前端 未结 3 1742
死守一世寂寞
死守一世寂寞 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:54

    I hope my little contribution can help anyone who can't display pdf preview in Firefox. I was using Primefaces 6 + Spring and I had the same problem but maybe not due the same reason. Indeed, I tried the proposed solution by Balus C. It helped me to display the pdf in Chrome and IE11 but it still was not working in Firefox 52.

    I noticed an error in the Firefox console: Load denied by X-Frame-Options: http://localhost:8080/myapp/ does not permit framing

    In my case, it was because spring-security configuration and the solution was edit spring-context.xml in this way:

    <sec:http ...>
    ...
    <sec:headers>        		
             <sec:frame-options policy="SAMEORIGIN" />
    </sec:headers>
    ...
    </sec:http>

    0 讨论(0)
  • 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:

    <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.

    0 讨论(0)
  • 2020-12-01 12:11

    It is not a browser or primefaces problem, just a funny getter problem.

    The getter is called twice by p:media (or if you refresh page than more times), but only the 1st call gets the correct data. StreamedContent encapsulates an InputStream, which has the property that it will give no bytes if the stream is at the end of the file. First time it is read to its end (data is ok), but every next call will get no data. :)

    javadoc of inputStream.read(): If no byte is available because the stream is at the end of the file, the value -1 is returned; otherwise, at least one byte is read and stored into b.

    Solution:

                private StreamedContent streamedContent;
                private InputStream stream;
    
    
                public void somewhere(){
                    byte[] b = ...
                    stream = new ByteArrayInputStream( b );
                    stream.mark(0); //remember to this position!
                    streamedContent = new DefaultStreamedContent(stream, "application/pdf");
                }
    
    
                public StreamedContent getStreamedContent() {
                    if (streamedContent != null)
                        streamedContent.getStream().reset(); //reset stream to the start position!
                    return streamedContent;
                }
    
    0 讨论(0)
提交回复
热议问题