When I try to load my page including a primefaces media pdf the PDF is not loaded. I generate the PDF in my postconstruct and keep the streamedcontent in a seperate variable. In my JSF I call the getStream method that returns the streamedcontent.
JSF page:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<ui:composition template="/templates/header.xhtml">
<ui:define name="content">
<f:metadata>
<f:viewParam name="invoiceID" value="#{invoiceBean.invoiceID}"/>
</f:metadata>
<ui:param name="invoiceID" value="#{invoiceBean.invoiceID}"/>
<h4 style="text-align: center;"><h:outputText
value="#{msgs['invoice.thankYou']}"/></h4>
<div class="card">
<p:media value="#{invoiceBean.stream}" player="pdf" width="100%" height="800px">
Your browser can't display pdf,
<h:outputLink
value="#{invoiceBean.streamedContent}">click
</h:outputLink>
to download pdf instead.
</p:media>
</div>
</ui:define>
</ui:composition>
</html>
Bean:
@Model
@Getter
@Setter
public class InvoiceBean {
@Inject
InvoiceService invoiceService;
@Inject
HttpServletRequest httpServletRequest;
private Invoice invoice;
private String invoiceID;
private StreamedContent streamedContent;
@PostConstruct
public void initInvoice() {
User user = getLoggedInUser();
invoiceID = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("invoiceID");
invoice = invoiceService.getInvoice(Long.parseLong(invoiceID));
PDFGenerator pdf = new PDFGenerator(invoice);
streamedContent = pdf.getStreamedContent();
}
public StreamedContent getStream() throws IOException{
FacesContext context = FacesContext.getCurrentInstance();
if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
return new DefaultStreamedContent();
} else {
return streamedContent;
}
}
}