7

I need to generate a PDF from a JSP JSF page. I have searched the net, but I didn't found any examples of how should I do this. I need to transform the whole page, or maybe only the charts that I have on that page.

P.S. I'm also using IceFaces.

user239161
  • 71
  • 1
  • 1
  • 2

3 Answers3

7

The easiest way is probably to capture the HTML using a Filter and convert that to PDF using a suitable API, then return the application/pdf data from the Filter. The IceFaces partial-submit support can probably be used to capture a subset of the component tree output, but you'll probably have to study the details of the IceFaces HTTP requests to figure out how to leverage that.

McDowell
  • 107,573
  • 31
  • 204
  • 267
  • 2
    That's indeed the way, but it's not as straightforward as it look like. For more detail: http://stackoverflow.com/questions/1963158/capture-generated-dynamic-content-at-server-side/1963571#1963571 – BalusC Dec 27 '09 at 21:14
2

As far as I know this isn't directly possible. You can use Jasper Reports to generate a pdf on the server side. Or, you can use PrintPDF which is a firefox plugin to create one from the web browser.

Mark Pope
  • 11,244
  • 10
  • 49
  • 59
  • +1 for jasper reports, and if you are using jfree chart for you charts, jasper reports will support them, and you can use the same charts in your report/pdf. – broschb Dec 27 '09 at 14:55
  • jasper uses iText to generate the PDF - unless there is another reason to use Jasper, it would be better to skip straight to iText – Kevin Day Dec 27 '09 at 14:57
2

You will probably need the following:

  1. Capture the (X)HTML output of your page. This can be done via the following code in a servlet of yours :

    InputStream is = new URL("http://localhost/foo/page.jsf").openStream();
    
  2. Transform the captured content to pdf. See the Flying Saucer renderer (and additionally - this thread)

  3. "Send" the generated pdf. That is, simply write (print) your pdf to the response.getOutputStream(), and set a Content-Type header - response.setContentType("application/pdf")

Community
  • 1
  • 1
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140