Show PDF Files With PrimeFaces

It is pretty easy to preview pdf files with PrimeFaces media component as explained in official page.

If you try this already there are two common problems that may find you.

  • Shows the same file every time
  • Cannot download the file multiple times (if you have an extra download button even the pdf viewer already have one)

To solve the first problem we generate an id for each media file to disable cache [1] .

The main reason of the second problem is that ‘a stream can be read only once’, so simply we have to refill the stream after every download attempt, but how can we understand that download is completed? Fortunately, PrimeFaces gives us some functions to probing download process [2] .

Here is the complete example:

import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
public class FormView {
private StreamedContent streamedContent;
private List<MyFile> files = new ArrayList<MyFile>();
private String selectedFileName;
public void init() {
MyFile myFile1 = new MyFile(1, "1.pdf");
MyFile myFile2 = new MyFile(2, "2.pdf");
public List<MyFile> getFiles() {
return files;
public String getSelectedFileName() {
return selectedFileName;
public void setSelectedFileName(String selectedFileName) {
this.selectedFileName = selectedFileName;
public StreamedContent getStreamedContent() {
return streamedContent;
public void setStreamedContent(StreamedContent streamedContent) {
this.streamedContent = streamedContent;
public void onRowSelect(SelectEvent event) {
String fileName = ((MyFile) event.getObject()).getName();
// we use this for refill the stream
selectedFileName = fileName;
private StreamedContent createStream(String fileName) {
streamedContent = new DefaultStreamedContent(getData(fileName), "application/pdf", "downloaded_" + fileName);
return streamedContent;
private InputStream getData(String fileName) {
// pdf files under src\main\resources
File file = new File(System.getProperty("user.dir") + "\\src\\main\\resources\\" + fileName);
InputStream is = null;
try {
is = new FileInputStream(file);
} catch (FileNotFoundException e) {
return is;
// refill the stream
public void refreshStream() {
public String generateRandomIdForNotCaching() {
return java.util.UUID.randomUUID().toString();
view raw hosted with ❤ by GitHub
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="" xmlns:h="" xmlns:f=""
xmlns:ui="" xmlns:p="">
<h2>Page No.1</h2>
<p:dataTable var="file" value="#{formView.files}" selectionMode="single" rowKey="#{}" style="width:50%">
<p:ajax event="rowSelect" listener="#{formView.onRowSelect}" oncomplete="PF('dlg1').show()" update=":form" />
<p:column headerText="Name">
<h:outputText value="#{}" />
<p:dialog header="Dialog" widgetVar="dlg1" modal="false">
<h:form id="form">
<p:messages id="msgs" />
<h:panelGrid columns="1" cellpadding="5">
<p:media value="#{formView.streamedContent}" player="pdf" width="600px" height="300px">
<f:param name="id" value="#{formView.generateRandomIdForNotCaching()}" />
<p:commandButton value="download" ajax="false" onclick="PrimeFaces.monitorDownload(startOffer, stopOffer);">
<p:fileDownload value="#{formView.streamedContent}" />
<p:remoteCommand name="refreshStreamOffer" actionListener="#{formView.refreshStream()}" />
<script type="text/javascript">
function startOffer() {
function stopOffer() {
view raw index.xhtml hosted with ❤ by GitHub
public class MyFile {
private Integer id;
private String name;
public MyFile(Integer id, String name) { = id; = name;
public Integer getId() {
return id;
public void setId(Integer id) { = id;
public String getName() {
return name;
public void setName(String name) { = name;
view raw hosted with ❤ by GitHub




2 thoughts on “Show PDF Files With PrimeFaces

  1. Stanley Shi says:

    Good explanation.
    Thank you!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: