How to Attach/Preview a PDF in Salesforce

Salesforce makes it extremely easy to generate PDF documents on the fly by simply using the renderAs=”pdf” attribute for the <apex:page> component.

It’s also a snap to attach these PDFs to records as Attachments. Below is a small Visualforce page and Controller that generates a PDF and saves it to an Account.

PDF Generator Visualforce Page

The Visualforce page allows the users to enter the ID of the Account to attach the PDF to as well as the name of the PDF.

<apex:page controller=“PdfGeneratorController”>

     <apex:sectionHeader title=“PDF Example” subtitle=“Attach a PDF”

           description=“Example of how to attach a PDF to a record.”/>

      <apex:form >

          <apex:pageBlock title=“PDF Input”>

               <apex:pageBlockButtons >

                    <apex:commandButton action=“{!savePdf}” value=“Save PDF”/>


               <apex:pageMessages />

               <apex:pageBlockSection >

                    <apex:pageBlockSectionItem >

                             <apex:outputLabel value=“File Name” for=“pdfName”/>

                         <apex:inputText value=“{!pdfName}” id=“pdfName”/>


                     <apex:pageBlockSectionItem >

                              <apex:outputLabel value=“Account ID” for=“id”/>

                          <apex:inputText value=“{!parentId}” id=“id”/>






PdfGeneratorController Custom Controller

The Controller passes the Account ID that the user entered as a parameter for the Visualforce page being generated. It then creates a new Attachment object and sets some attributes. It sets the ParentId to the value of the Account ID that the user entered so that the PDF is attached to that record.

The Body of the attachment uses the Blob returned from the PageReference’s getContent method. You could also use the getContentAsPDF method which always returns the page as a PDF, regardless of the <apex:page> component’s renderAs attribute.

However, this method always seems to throw an error in the test class. See the PageReference documentation for more info. The method then redirects the user to the Account page so they can view the PDF attachment.

public PageReference savePdf() {

PageReference pdf = Page.PdfGeneratorTemplate;
// add parent id to the parameters for standardcontroller

// create the new attachment
Attachment attach = new Attachment();

// the contents of the attachment from the pdf
Blob body;

try {

// returns the output of the page as a PDF
body = pdf.getContent();

// need to pass unit test — current bug
} catch (VisualforceException e) {
body = Blob.valueOf(‘Some Text’);

attach.Body = body;
// add the user entered name
attach.Name = pdfName;
attach.IsPrivate = false;
// attach the pdf to the account
attach.ParentId = parentId;
insert attach;

// send the user to the account to view results
return new PageReference(‘/’+parentId);



PDF Generator Template Visualforce Page

This is the Visualforce page that is generated in the Controller. It simply uses the StandardController and displays the Account name for the ID passed to it.

<apex:page standardController=”Account” renderAs=”pdf”>
<p>You created a PDF for {!}</p>

Test Class

private class Test_PdfGeneratorController {

static Account account;

static {

account = new Account();
account.Name = ‘Test Account’;
insert account;


static testMethod void testPdfGenerator() {

PageReference pref = Page.PdfGenerator;

PdfGeneratorController con = new PdfGeneratorController();


// populate the field with values
con.parentId =;
con.pdfName = ‘My Test PDF’;

// submit the record
pref = con.savePdf();

// assert that they were sent to the correct page

// assert that an attachment exists for the record
System.assertEquals(1,[select count() from attachment where parentId =]);



This blog is very useful to learn about how to add a pdf to a record in salesforce.