Render a Visualforce Page as PDF from Apex
This page is a simple user interface. When you’re generating a PDF file from Apex, all the action is in the Apex code.
This Apex controller can be conceptually divided into four parts.
- The three public properties at the beginning capture the values submitted by the three input elements on the form.
- The sendReport() action method fires when the Send Account Summary button is clicked.
- The two public helper properties supply the values to use in the two select list input elements.
- The private helpers at the end encapsulate the list of possible PDF report formats. You can add your own report by creating a Visualforce page and then adding an entry for it in this section.
When the sendReport() action method fires, the code does the following.
- It performs rudimentary error checking to ensure that the form fields have useful values.
- Next it uses the value of the selected account to look up the name of that account. The account name is used in text that’s added to the email message. This lookup is also an opportunity to further validate the form value and ensure that a real account was selected.
- It uses the Messaging.SingleEmailMessage class to assemble an email message, setting the To, Subject, and Body email message values.
- The code creates a PageReference for the selected report format and then sets a page request parameter on it. The parameter is named “id”, and its value is set to the selected account’s ID. This PageReference represents a specific request to access this page in the context of the specified account. When getContentAsPdf() is called, the referenced Visualforce page has access to the specified account, and the page is rendered with that account’s details.
- Finally, the PDF data is added to an attachment, and the attachment is added to the email message created earlier. The message is then sent.
When using PageReference.getContentAsPdf(), the return type of the method call is Blob, which stands for “binary large object.” In Apex, the Blob data type represents untyped binary data. It’s only when the reportPdf variable is added to the Messaging.EmailFileAttachment with a content type of “application/pdf” that the binary data becomes a PDF file.
In addition, the call to getContentAsPdf() is wrapped in a try/catch block. If the call fails, the catch replaces the hoped for PDF data with a Blob version of the exception’s message text.
Rendering a Visualforce page as PDF data is treated semantically as a callout to an external service for various reasons. One reason is that the rendering service can fail in all the same ways that an external service can fail. For instance, the page references external resources that aren’t available. Another example is when the page contains too much data—usually in the form of images—or the rendering time exceeds a limit. For this reason, always wrap the getContentAsPdf() rendering call in a try/catch block when rendering a Visualforce page as PDF data in Apex.