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" />


Done!!

7 comments:

  1. i am getting this exception

    java.lang.IllegalArgumentException: pdf is not a valid media player type
    org.primefaces.component.media.player.MediaPlayerFactory.getPlayer(MediaPlayerFactory.java:52)
    org.primefaces.component.media.MediaRenderer.resolvePlayer(MediaRenderer.java:119)
    org.primefaces.component.media.MediaRenderer.encodeEnd(MediaRenderer.java:41)
    javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:905)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1786)
    javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
    javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:875)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
    com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:439)
    com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:124)
    com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)

    ReplyDelete
    Replies
    1. what version of PF you are using?
      I checked the PF tag doc for 3.x, 4.x, 5.x
      below are the description for player attribute

      Type of the player, possible values are
      "quicktime","windows","flash","real" and "pdf".

      Delete
  2. java.lang.NoClassDefFoundError: javax/el/ValueReference
    at org.primefaces.el.InterceptingResolver.setValue(InterceptingResolver.java:28)
    at com.sun.el.parser.AstValue.setValue(AstValue.java:167)
    at com.sun.el.ValueExpressionImpl.setValue(ValueExpressionImpl.java:273)
    at com.sun.faces.facelets.el.TagValueExpression.setValue(TagValueExpression.java:131)
    at org.primefaces.el.ValueExpressionAnalyzer.getExpression(ValueExpressionAnalyzer.java:49)
    at org.primefaces.util.DynamicResourceBuilder.build(DynamicResourceBuilder.java:49)
    at org.primefaces.component.media.MediaRenderer.getMediaSrc(MediaRenderer.java:125)
    at org.primefaces.component.media.MediaRenderer.encodeEnd(MediaRenderer.java:42)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:894)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:443)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)

    ReplyDelete
  3. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! cfa level 1 mock exam pdf with answers

    ReplyDelete
  4. Enjoyed every bit of your blog post. Really looking forward to read more. Fantastic.
    share files

    ReplyDelete
  5. I really like your post. You do a fantastic job. I am thankful for you sharing this knowledge with us. I hope that was of use to you. Additionally, I'll provide you with access to the most recent blog spacebar counter. Space Bar Test allows you to test your finger speed on the space bar. You can also calculate your maximum tapping speed.

    ReplyDelete
  6. The creation, altering and encryption of PDF record are conceivable with various sorts of converter devices on the lookout.
    PDF to PNG

    ReplyDelete

LinkWithin

Related Posts Plugin for WordPress, Blogger...