It's not a secret anymore!
The moose likes JSF and the fly likes Return PDF via Response Stream. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Return PDF via Response Stream." Watch "Return PDF via Response Stream." New topic

Return PDF via Response Stream.

James Clark

Joined: May 10, 2010
Posts: 3
Hi All,

My first post here but I can see myself frequenting as I battle with JSF!

I have a page whereby people search for reports and the results are returned to them in a datatable. They are first faced with some input input controls to refine their search, they click search and control goes to a session backing bean. The backing bean gets the records from the database, populates a list, sets a boolean value to true and then reloads the page. The boolean value causes a previously non-rendered datatable to be displayed. Each row will have contain a picture of a PDF. Upon clicking this PDF, the user should be given a download prompt and be able to download the corresponding PDF.

This all works fine, however after the PDF has been downloaded, I cannot continue to use the page and must navigate to a different page and then return. If I use other controls within the same form, I get the download prompt again for the same file, as well as an exception thrown:

Here is the code I am using to return the PDF through the response:

The PDF pictures which prompt the download when clicked are surrounded by a command link which has an action listener which sets PDFArchiveChosen. The action for the link is the method shown above, which streams the PDF to the user.

What I want is for users to click on a PDF picture and download the corresponding file. Once they've done that, I want them to be able to use the page as normal: run different searches, click other pdf pictures to get different files downloaded etc.

Abdullah Dokuyucu

Joined: Apr 27, 2010
Posts: 24

Hello, did you try using a normal <h:link> ?

Freelancer for life!
James Clark

Joined: May 10, 2010
Posts: 3
Ekrem Dkyc wrote:Hello, did you try using a normal <h:link> ?

Hi Ekrem, thanks for the reply.

At first I thought this wouldn't help as <h:link> (I only had <hutputLink> available), does not have action, actionListener attributes so I wouldn't be able to link it with the backing bean in order to set the report chosen and then pull the PDF from the Database and then stream it back to the user. I thought I'd give it a go though.

I included a hidden input tag which linked to the backing bean and used the onclick event of the <hutputLink> to set its value. In the same javascript function I then forced it to "click" a button with an action event which started streaming the PDF, using the value set to the hidden input.

I didn't think this would work as it is still using a commandButton with an action event to initiate streaming. It seems to work great though! I'm not sure why - the difference seems to be that I'm not using an actionListener and that the link clicked is outside the Datatable - which I suspect is the main offender.

Anyway, thanks for your help.
Don't get me started about those stupid light bulbs.
subject: Return PDF via Response Stream.
jQuery in Action, 3rd edition