9

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 content, put data in session, and send Javascript to be executed (window.open to open new page to show PDF)
  2. On opened page I just have p:media tag inside h:body with value pointing to StreamedContent:

Now, on that page my PDF is not generated. In log I can see these two lines:

org.primefaces.application.PrimeResourceHandler handleResourceRequest
SEVERE: Error in streaming dynamic resource. Expression cannot be null

I started to debug and find out a few things.

First, I added breakpoint to @PostConstruct method of my RequestScoped bean. What is interesting is that breakpoint is reached twice, and to my big surprise after that PDF is shown perfectly?!

After some debugging through PrimeResourceHandler I figure out that in some cases ValueExpression is not calculated, in fact it throws NullPointerException, and again while debugging I saw that two requests are sent, and second request fails because dynamicContentId is removed in first request, and second call to handleResourceRequest doesn't have sense.

Through Firebug I can see two requests, first which is good with PDF data, and second which is also with content-type application/pdf but empty, with size 0.

xhtml page:

<html>
  <h:head></h:head>
  <h:body>
    <p:media value="#{reportBean.streamedContent}" player="pdf" width="500" height="500"/>
  </h:body>
</html>

backing bean:

@RequestScoped
public class StampaListeBackingBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private StreamedContent streamedContent;

    @PostConstruct
    public void init() {
        Map<String, Object> session = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        byte[] b = (byte[]) session.get("reportBytes");
        if (b != null) {
            streamedContent = new DefaultStreamedContent(new ByteArrayInputStream(b), "application/pdf");
        }
    }

    public StreamedContent getStreamedContent() {
        if (FacesContext.getCurrentInstance().getRenderResponse()) {
            return new DefaultStreamedContent();
        } else {
            return streamedContent;
        }
}

    public void setStreamedContent(StreamedContent streamedContent) {
        this.streamedContent = streamedContent;
    }
}

I need to understand why two requests are sent on page with p:media tag, and to figure out how to make this work. Backing bean is request scoped, it creates StreamedContent in @PostConstruct method, and has getter and setter for that field. Primefaces version is 3.4.2, with Mojarra 2.1.14.

ADDED:

It is easy to reproduce my problem. If code in init method is replaced with following:

FileInputStream fis = new FileInputStream(new File("C:\\samplexxx.pdf"));
streamedContent = new DefaultStreamedContent(fis, "application/pdf");

problem can be reproduced.

partlov
  • 13,789
  • 6
  • 63
  • 82
  • As to the observed bean behavior, this is known from `` and answered in detail among others here: http://stackoverflow.com/questions/8207325/display-image-from-database-with-pgraphicimage/12452144#12452144 As to the observed exception, sorry, no idea. Some concrete code would be helpful. – BalusC Jan 23 '13 at 16:37
  • @BalusC I added xhtml and java code, it is very simple. I also added my inspection from Firebug. – partlov Jan 23 '13 at 16:46
  • What if you use a static path like ``? Just to exclude resource servlet from being the cause. And how about PrimeFaces own showcase example? Does it work for you too? Just to exclude browser from being the cause (it works for me) http://www.primefaces.org/showcase-labs/ui/media.jsf – BalusC Jan 23 '13 at 20:24
  • @BalusC It works perfectly. Primefaces examples also works fine. But as I can see they also use static paths in all examples. I also experimented with different scopes of bean, but it didn't help. – partlov Jan 23 '13 at 20:28

3 Answers3

10

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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    :) Thank you BalusC. This will be OK for now. This teach me not to always rely on framework widgets, for every simple thing. – partlov Jan 23 '13 at 20:59
  • I think that real problem is in `PrimeResourceHandler.handleResourceRequest`, in finally part is this line `session.remove(dynamicContentId)`. That is maybe a point where these two requests have race condition. – partlov Jan 23 '13 at 21:06
  • With the servlet workaround, Firefox sends only 1 request on the PDF instead of 2. So the problem is actually in the response of the 1st request; the browser somehow couldn't deal with it. If I have more time I'd investigate it, but not now :) – BalusC Jan 23 '13 at 21:08
  • Thank you BalusC. -Although, its sad that in 2015, we have to resort to "workaround" for IE11. (IE11 was still getting the "must start with '%PDF-' error, so I am using your servlet technique) – sairn Jan 28 '15 at 15:48
  • thanks baluc for your post, your post works with IE8 with jsf 2.1 and 2.1.2, but does not works in IE8 with javax.faces-2.2.7. Please help. – user1503117 Feb 19 '15 at 18:33
  • In IE8 the doGet is called twice and it is problem for me because I'm clearing the reportBytes attribute after writing it to the response..@BalusC do you know why it is called twice? Thank you! – Tobia Zambon Sep 15 '16 at 15:36
6

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;
            }
steve
  • 615
  • 6
  • 14
3

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>
jmoran
  • 164
  • 1
  • 5
  • Very usefull. Thx, in my case I used java config: `http.headers() .frameOptions().sameOrigin() .httpStrictTransportSecurity().disable();` – UHDante Oct 04 '18 at 12:14