• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to export the JSF page to PDF?

 
Omar Ali
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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
 
Tim Holloway
Bartender
Posts: 18469
61
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks for your valueable information.

What libraries do you recommend to read about? Are there any examples?
 
Tim Holloway
Bartender
Posts: 18469
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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
Bartender
Posts: 18469
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Hug your destiny! And hug this tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!