It turns out that you can dynamically control whether a Visualforce page is rendered as a PDF or not.
Visualforce (coming in a week or two) has this neat feature where you can just set an attribute and have the page rendered as a PDF. This pretty much means you can PDF anything on the platform. What I wanted to do was have a page rendered as standard HTML though, and flick a toggle to have it rendered as PDF. Same page, two output formats. (I’m trying to find a cool demo for this webinar, where I’ll be helping Ron).
The code for my Visualforce page was pretty straightforward. You can see a picture of the result above.
Here’s the output PDF it generates.
Here’s how I did it. I’ve called this page
There are two things happening here. The PDF link will make a call out to the
deliverAsPDF() method in my controller, and the
renderAs attribute (valid choices: “pdf” or null) will make a call out to the
In other words, what I’m going to do is this: when you push the link, I’m going to grab the page, but this time only after ensuring that the
renderAs attribute is set to “pdf”. I’m then going to return the PDF, and it will pop up in a new window (
target attribute). The way I set the attribute is based on whether a parameter has been passed to the page or not, as you’ll see in the code:
That’s it! I guess another key thing to realize here in the MVC model is that I can actually manipulate pages from my controller with Apex. ie.
Page.foo is a reference to my Visualforce page, and I can do things with it (in this case, pass it parameters).
I believe you can dynamically assign style sheets as well, so you can imagine doing some cool stuff with this like having CSS for the print layout that arranges things more pleasantly etc.