Forum How do I...?

Current (Icefaces)Page to Pdf

spatnaik
Hi

My application uses Icefaces(1.6.1_21) at the front end.

All the pages are generated dynamically from xhtml templates.

I need to generate pdf and display in a new window to the user on click of a 'Export' button. Pdf should capture information on the current page.

I am wondering if the Java interface of Prince is capable of doing this. I know there is a convert(InputStream xmlInput, OutputStream pdfOutput) method. But I am not sure if am doing it right. :(

I am doing the following in my managed bean(The code might look dumb !! ).

FacesContext context = FacesContext.getCurrentInstance();

HttpServletResponse response =
(HttpServletResponse)context.getExternalContext().getResponse();

HttpServletRequest request =

(HttpServletRequest) context.getExternalContext().getRequest();

boolean converted = prince.convert(request.getInputStream(), response.response.getOutputStream());

I am new to Icefaces and Prince. Any help is greatly appreciated.

Thanks,
Smita
mikeday
Getting the Prince convert() method to write to your response output stream looks good, that way the generated PDF will go back to the browser. You probably want to set the content-type of the response to application/pdf before calling convert(), though, so the browser knows it's PDF and not HTML.

I'm a bit suspicious about passing your request input stream to Prince, that looks wrong. The request input stream gives you access to the body of the client request, such as data submitted by a HTTP POST from a form in a web page. But I think what you want is to get the output of the current page, saving it in a buffer such as a ByteArrayOutputStream instead of returning it to the browser, and then passing it to Prince and returning the generated PDF to the browser, is that correct?
spatnaik
Thank you so much for such a prompt response.
You are absolutely correct. I need to get and save the output of the current page and then pass that to prince to generate the pdf. Now I need to figure how I should do that.
mikeday
If your class is generating the output and writing it to a stream, such as the response output stream, then you could always pass in a ByteArrayOutputStream instead and capture the data like that.

If it's more complicated than this, then you might need to create a proxy class that wraps up the HttpServletResponse class and overrides getOutputStream(). There are probably existing class libraries that can offer something like this, but I don't know if they support ICEfaces.