Send PDF as attachment using Visualforce Page

Usecase- Email a list of Account details as a pdf.

Visualforce Page -

<apex:page Controller="pdfasattachment" renderAs="{!if($CurrentPage.parameters.p == null, null, 'pdf')}" >

<apex:pageBlock title="Hello {!$User.FirstName}!">
You are displaying contacts from the account  .
Click a contacts name to view his or her details.
</apex:pageBlock>
<apex:pageBlock title="Contacts">
<apex:form >
<apex:dataTable value="{!accounts}" var="contact" cellPadding="4" border="1">
<apex:column >
<apex:commandLink rerender="detail">
{!contact.Name}

<apex:param name="cid" value="{!contact.id}"/>
</apex:commandLink>
</apex:column>
<
<apex:column >
<apex:commandLink rerender="detail">
{!contact.id}

<apex:param name="cid" value="{!contact.id}"/>
</apex:commandLink>
</apex:column>
</apex:dataTable>
</apex:form>
</apex:pageBlock>
<apex:outputPanel id="detail">
<apex:actionStatus startText="Requesting...">
<apex:facet name="stop">
<apex:detail subject="{!$CurrentPage.parameters.cid}"
relatedList="false" title="false"/>
</apex:facet>
</apex:actionStatus>
</apex:outputPanel>
  <apex:form >
   <apex:commandLink rendered="{!$CurrentPage.parameters.p == null}"  value="PDF" 
            action="{!DeliverAsPDF}" ></apex:commandLink>
  </apex:form>

</apex:page>

Controller

public class pdfasattachment{

    public PageReference DeliverAsPDF() {
       // Reference the page, pass in a parameter to force PDF
     PageReference pdf =  Page.pdfasattachment;
     pdf.getParameters().put('p','p'); 
     pdf.setRedirect(true);
     // Grab it! 
     //Blob b = pdf.getContent();
     Blob b = pdf.getContentAsPDF();
     // Create an email
     Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
     email.setSubject('From getDeliverAsPDF!');
     String [] toAddresses = new String[] {'rpatnaik@testdomain.com'};
     email.setToAddresses(toAddresses);
     email.setPlainTextBody('Here is the body of the email');
     // Create an email attachment
     Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
     efa.setFileName('MyPDF.pdf'); // neat - set name of PDF
     efa.setBody(b); //attach the PDF
     email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
     // send it, ignoring any errors (bad!)
     Messaging.SendEmailResult [] r = 
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});   
     return null;
    }

    public String getAccount() {
        return null;
    }
                    
        List<Account> accounts;

        public List<Account> getAccounts() {
                if(accounts == null)
                accounts = [select name,id from account limit 10];
                return accounts;
        }
        public PageReference getDeliverAsPDF() {
        return null;
     
 }

}

Output


















Result













The attachment was received as a PDF






Comments

Popular posts from this blog

Introduction to Salesforce Agent Script: Build Predictable AI Agents

Anti-Patterns in Salesforce Agentforce: What to Avoid When Building AI Agents

MCP vs. Traditional APIs: Choosing the Right Integration Approach