Attach a PDF to a Record in Salesforce

Salesforce.com makes it extremely easy to generate PDF documents on the fly by simply using the renderAs="pdf" attribute for the 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.</p>

Note: there is a small issue when it comes to testing the Controller. Salesforce currently throws an error (Salesforce.com Error "Unable to retrieve object") when getContent or getContentAsPDF is called from a test method. There's an Idea to make this work properly. I encourage everyone to vote for this.

PdfGenerator 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. You could just have easily used a Controller Extension instead of entering the ID for the Account but I went this route for simplicity.

<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:pageBlockButtons>
 <apex:pageMessages />
  
 <apex:pageBlockSection >
  
  <apex:pageBlockSectionItem >
  <apex:outputLabel value="File Name" for="pdfName"/>
   <apex:inputText value="{!pdfName}" id="pdfName"/>
  </apex:pageBlockSectionItem>
  
  <apex:pageBlockSectionItem >
  <apex:outputLabel value="Account ID" for="id"/>
   <apex:inputText value="{!parentId}" id="id"/>
  </apex:pageBlockSectionItem>
  
 </apex:pageBlockSection>

  </apex:pageBlock>
 </apex:form>

</apex:page>

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 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.</p>

public with sharing class PdfGeneratorController {

 public ID parentId {get;set;}
 public String pdfName {get;set;}
 
 public PageReference savePdf() {

  PageReference pdf = Page.PdfGeneratorTemplate;
  // add parent id to the parameters for standardcontroller
  pdf.getParameters().put('id',parentId);

  // 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);

 }

}

PdfGeneratorTemplate 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">
 <h1>Congratulations!!</h1>
 <p>You created a PDF for {!account.name}</p>
</apex:page>

Test Class

@isTest
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;
  pref.getParameters().put('id',account.id);
  Test.setCurrentPage(pref);
  
  PdfGeneratorController con = new PdfGeneratorController();  
  
  Test.startTest();
  
  // populate the field with values
  con.parentId = account.id;
  con.pdfName = 'My Test PDF';

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

  // assert that they were sent to the correct page
  System.assertEquals(pref.getUrl(),'/'+account.id);

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

 }
}