function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Apex Class for Outbound Email with VF page rendeded as PDF - Attachment is blank

I created an apex class for sending outbound emails on a custom objec.t  The class is supposed to referenced a visualforce page rendered as pdf and attach it to the email.


I created the visualforce page and the pdf renders correctly when I view it in  But when I send the email with the pdf attached, the pdf shows up as an attachment, but it is completely blank.


I think this has something to do with the apex class not passing the correct id to the VF page, but I'm not sure.  Any idea why this isn't working? 



public class EmailDraft

private Email_Draft__c ed;
public EmailDraft(ApexPages.StandardController controller)

public PageReference SendEmail()

// Reference the attachment page, pass in the account ID  
        PageReference pdf =  Page.VPL_Attach;

        // Take the PDF content 
        Blob b = pdf.getContent();

        // Create the email attachment 
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {};
IF( != null) {
if( != null){
String[] ccAddresses = new String[] {};
mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

ed.approval_status__c = 'Approved & Sent';
update ed;

     PageReference next = new PageReference('/apex/email_draft_detail?id=';
     return next;


Ritesh AswaneyRitesh Aswaney

It would seem that you are using this class as a Controller Extension.


In which case can you check if you're passing in the Id to the VF Page which invokes this, as the controller.getRecord() will return a record, only if the Id was passed in as a Query Parameter in the initial URL.


Also, might be an idea to put some debug statements to check if you're getting a null record all through.


I have been trying to do this without success for the last three hours.  Do you have any sample code you can post or can you give me a little more guidance on how to do this?


Thanks again!

Ritesh AswaneyRitesh Aswaney

Here's a great example that should sort you out :


good luck ! :)