I'm trying to show inline PDF which is opened in new browser window. I have following scenario:
- 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) On opened page I just have
p:media
tag insideh:body
with value pointing toStreamedContent
:
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.