+ Start a Discussion
Bob_zBob_z 

How to Attach a Dynamic PDF to a Salesforce Messaging

I am using an example of how to attach a pdf through an email template from this link https://www.codeproject.com/Tips/1129916/How-to-Attach-a-Dynamic-PDF-to-a-Salesforce-Messag and trying to adapt it to a custom object I have but when i send an email I get a blank pdf file. There is only one field that should be on there but I wanted to test this to see if it will work before i add more fields to the code. My code is below. If anyone could help me I would appreciate it. I know I'm missing something simple. 

email template
<messaging:emailTemplate subject="Invoice Attached" recipientType="User" relatedToType="Request_for_Special_Payment__c">

<messaging:attachment renderAs="PDF" filename="Invoice.pdf">
        <c:IncludeAttachments rId="{!relatedTo.Id}"/>
    </messaging:attachment>
    <messaging:htmlEmailBody >
    <html xmlns="http://www.w3.org/1999/xhtml">
        Please find your invoice attached.
    </html>
    </messaging:htmlEmailBody>
</messaging:emailTemplate>
Apex Class
IncludeAttachmentsController
global class IncludeAttachmentsController {

/* Variables and Constants */

global String PageContents{ get; set; }
global String rIdObjectId{ get; set {
    UpdateContents(value);
} }

public void UpdateContents(String rIdObjectId) 
{
    try {
        PageReference pageRef = Page.Invoice;
        pageRef.getParameters().put('rId', rIdObjectId);
    
        PageContents = pageRef.getContent().toString().replace
        ('<html style="display:none !important;">', '<html>');
    } catch(exception ex) { 
        PageContents = 'An error has occurred while trying to generate this invoice.  Please contact customer service.';
    }
}
}
Visualforce Component
IncludeAttachments
<apex:component controller="IncludeAttachmentsController" access="global">
    <apex:attribute name="rId"

        description="rId Id"

        assignTo="{!rIdObjectId}"

        type="Id" />

    <apex:outputText value="{!PageContents}" escape="false" />
</apex:component>



 
Bob_zBob_z
Hello!
Francesca ColazzoFrancesca Colazzo
Any news? I have the same issue.