• WHO WE ARE
  • WHAT WE DO
    • Salesforce
      • Implementations
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service Lightning
        • Field Service for SMEs
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • Data Science
      • BI Solutions
      • AI/ML solutions
      • Agentic AI
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG
    • WHO WE ARE
    • WHAT WE DO
      • Salesforce
        • Implementations
          • Sales Cloud
          • Service Cloud
          • CPQ
          • Field Service Lightning
          • Field Service for SMEs
        • Developments
          • Salesforce Customization
          • Custom Application Development
          • AppExchange Product Development
        • Migrations
          • Classic to Lightning Migration
          • Other Systems to Salesforce Migration
        • Integrations
      • Data Science
        • BI Solutions
        • AI/ML solutions
        • Agentic AI
    • HOW WE DO
      • Delivery Model
      • Our Works
    • REACH US
      • Contact Us
      • Careers
    • BLOG
  • [email protected]
  • (+91) 44-49521562
Merfantz - Salesforce Solutions for SMEs
Merfantz - Salesforce Solutions for SMEs
  • WHO WE ARE
  • WHAT WE DO
    • Salesforce
      • Implementations
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service Lightning
        • Field Service for SMEs
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • Data Science
      • BI Solutions
      • AI/ML solutions
      • Agentic AI
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG

How to Attach/Preview a PDF in Salesforce

  • December 7, 2018
  • Merfantz Editor
  • Salesforce Developments Tutorial
  • 0

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

}

}

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”>
<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();

}
}

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

Author Bio

Merfantz Editor
+ Recent Posts
  • August 4, 2023
    How to Setup CRM Analytics for Financial Services Cloud
  • August 2, 2023
    How to Get Started with Salesforce Hyperforce - Merfantz
  • July 28, 2023
    Health Cloud Mastery: Salesforce Trailhead for Effective Healthcare CRM
  • July 26, 2023
    How to integrate Data cloud and Marketing cloud In Salesforce
  • Previous How to Check API Usage in Salesforce
  • Next How to Create a Report Based on Field History Tracking in Salesforce
Merfantz Technologies is a leading Salesforce consulting firm dedicated to helping small and medium enterprises transform their operations and achieve their goals through the use of the Salesforce platform. Contact us today to learn more about our services and how we can help your business thrive.

Discover More

Terms and Conditions
Privacy Policy
Cancellation & Refund Policy

Contact Info

  • No 96, 2nd Floor, Greeta Tech Park, VSI Industrial Estate, Perungudi, Chennai 600 096, Tamil Nadu, INDIA
  • (+91) 44-49521562
  • [email protected]
  • 9:30 IST - 18:30 IST

Latest Posts

Mastering Attachment Compression for Salesforce Developers May 9, 2025
Salesforce API Integrations: Connect with Slack, Zoom, and Teams May 2, 2025
Guide to Streamlining Sales Success
Mastering Salesforce CPQ: A Comprehensive Guide to Streamlining Sales Success April 17, 2025

Copyright @2023 Merfantz Technologies, All rights reserved