• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

How to export the JSF page to PDF?

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I need your help and guidance on how to export a full jsf page to PDF. In my jsf page, I have a button to export to PDF and some fields where I am retrieving the values from a database. The form is ready but I don't know how to program code or program a button to export the page to PDF. I tried to search for an easy and a standard way but unfortunately I didn't find any useful example or code. Could you please instruct and to guide me on how to this.

Thanks
 
Saloon Keeper
Posts: 28753
211
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strictly speaking, one would render a JSF View as PDF using an alternative renderer instead of the default HTML renderer.

However, it's more likely that like most people you don't really want to "export the JSF page to PDF", you want to generate a downloadable document in PDF form for viewing, printing and/or saving on the client's machine. Which is entirely different.

And you probably don't want the PDF to literally resemble the screen display, you probably want to construct the PDF to look better as a printed document.

If you literally wanted the screen display in PDF form, Linux (and probably Mac) users can simply use their browser's "Print as PDF" printer option. I think that this MIGHT be available in newer versions of Windows, as well, but if not, there are 3d-party printer drivers that can do the job.

However, if you want to generate a printer-friendly PDF document, don't do it in JSF. Write a standard (non-JSF) servlet and use one of the available PDF rendering libraries to render the servlet output stream as a PDF. Pass any data you need to exchange between JSF and the servlet in session- or application-scope objects.

JSF wasn't intended to be a do-everything framework, and PDF's are one of the things that it wasn't intended to do.
 
Omar Ali
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your valueable information.

What libraries do you recommend to read about? Are there any examples?
 
Tim Holloway
Saloon Keeper
Posts: 28753
211
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The iText library is very popular and you should have no problems finding example code.

I often use Apache FOP, which allows me to use an XML template for the document instead of coding the document elements in logic.
 
Omar Ali
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks

What do you recommend for beginners? Is it Itext or Apache Fob?

The PDFs will contain only text, 2 tables and few fields to be retrieved from the database?
 
Tim Holloway
Saloon Keeper
Posts: 28753
211
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whichever you prefer. There's no "best" solution. PDF's aren't very simple to begin with, anyway. FOP allows you to define the page prototype as an XML document and then invoke a translator to convert the document to PDF. The iText library is an API that you can use to build PDFs up manually element by element.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic