Apex Pattern: Save and Open PDF from button

I’ve heard variations to this request a few times now:

Can we have a button on the {object} where we generate a PDF, save it as an attachment on the {object} record, and open it?

Saving a PDF is so simple with Visualforce it’s ridiculous, simply utilize <apex:page> with the attribute renderAs=”pdf”. In fact, this is one of the early things that made me fall in love with the platform.

Here’s a simple pattern / template I built recently for being able to generate a PDF, save it, and open it all with one button click. This doesn’t contain any specific business logic or content in the PDF, feel free to use this and customize it to your heart’s desire! It could use some error checking and better tests before it’s production ready.

Here’s a demo of it in action:

Unable to display content. Adobe Flash is required.

Here’s the code:


This entry was tagged , , , . Bookmark the permalink.