Media is a generic player to embed multimedia content. Players are flash, quicktime, windows media, realplayer and pdf; one is chosen from the file extension or set with player. The demo displays a PDF from a static path and the same PDF as StreamedContent; the content inside the tag is the fallback for browsers that cannot display the PDF.
<div class="card">
<h5>PDF</h5>
<p:media value="/resources/demo/media/guide.pdf" width="100%" height="300px">
Your browser can't display pdf, <h:outputLink
value="#{request.contextPath}/resources/demo/media/guide.pdf">click</h:outputLink> to download pdf instead.
</p:media>
</div>
<div class="card">
<h5>PDF via StreamedContent</h5>
<p:media value="#{mediaView.pdf}" width="100%" height="300px" player="pdf">
Your browser can't display pdf, <h:outputLink
value="#{request.contextPath}/resources/demo/media/guide.pdf">click</h:outputLink> to download pdf instead.
</p:media>
</div>
package org.primefaces.showcase.view.multimedia;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import jakarta.enterprise.context.RequestScoped;
import jakarta.faces.context.FacesContext;
import jakarta.inject.Named;
@Named
@RequestScoped
public class MediaView {
public StreamedContent getPdf() {
return DefaultStreamedContent.builder()
.contentType("application/pdf")
.name("guide.pdf")
.stream(() -> {
try {
FacesContext facesContext = FacesContext.getCurrentInstance();
return facesContext.getExternalContext().getResourceAsStream("/resources/demo/media/guide.pdf");
}
catch (Exception e) {
e.printStackTrace();
return null;
}
})
.build();
}
}