Tuesday, October 21, 2014

How to display dynamic PDF document with <p:media /> component

From the PrimeFaces showcase, the media component used to display PDF document
But the showcase uses a static PDF document.

In real life, we might want to display different PDF documents by different use cases,
or a runtime generated PDF document.

To achieve the above purpose, we need to use the value attribute in <p:media /> component by passing in a StreamedContent from backingBean.

1. BackingBean method that return StreamedContent.
public StreamedContent getPdfDocument() {  

     // logic to generate the PDF  

    return new DefaultStreamedContent(pdfInByteArrayInputStream, "application/pdf");

2. display the PDF content with <p:media /> component.

<p:media value="#{myBean.pdfDocument}" width="100%" height="500px" player="pdf" />



Related Posts Plugin for WordPress, Blogger...