Wednesday, July 16, 2008

How do you generate a PDF file from a JSP Java Server Page over the web?

I often get asked how to generate a PDF on the fly and deliver it to a client. Essentially, you need a tool that takes text and does the PDF generation, something like Apache FOP (Formatting Objects Processor) or iText, and then send a link to the generated file back to the client. When the client clicks on the link, the PDF file is displayed.

I guess you could wait a bit and embed the generated pdf file in the current JSP page and send that page back to the client also...That would work if the pdf can be generated on the sever quickly enough.

Here's a post from Bill Brogden from the JavaRanch:

Converting HTML to PDF Portable Dococument Format on the Fly within a JSP

"You can't generate PDF from a JSP template. You will have to generate it with a specific PDF generating tool such as FOP or iText and give a link to the binary data in a link to a servlet that can formulate the response correctly. Remember, JSP is for HTML formatted character data, binary data should be handled by servlets."

No comments: