I blogged the other day about how to dynamically choose whether a Visualforce page is rendered as a PDF or not-useful when creating invoices and so on.  Now I’m going to email that page using a little Apex code.

First, let’s define the page:

This is a refinement of the one I blogged last time, using some tips from Doug Chasman.  Note that the outputlink won’t render in the final PDF – cool!  The only key thing here is that the page will render as a PDF if a parameter “p” is passed in. Otherwise it renders as HTML.

The question is how to write our getDeliverAsPDF() method to create the PDF and mail it off in an email.  Here’s how I coded the controller to do this:

The only tricky bit here is the pdf.GetContent() method, which grabs the rendered PDF content.  I then stuff it into an email attachment and send it off!

Technorati Tags:

Get the latest Salesforce Developer blog posts and podcast episodes via Slack or RSS.

Add to Slack Subscribe to RSS