Sneaky Little Snippets

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] .