I am trying to come up with a flexible system to attached donation receipts to emails. Right now I have the pdf html in two places:

  1. inside in the visualforce email
  2. Inside another visualforce page

1 - is so that we can email receipts 2 - is so we can view them and print them from the browser.

The inherent problem here is duplicating the pdf code. I want to include the visual page in the email template but it doesn't look like Salesforce passes the ID to the apex:include so it doesn't work :(

Any ideas?

    <messaging:attachment renderAs="pdf" filename="Your Donation Receipt - {!relatedTo.Date_Received_in_Bank__c}" >
  <apex:include pageName="ReceiptLetterPdfCode" />

3 Answers 3


I think I have the answer. You cannot pass ideas for Apex:include and you cannot use standard controllers in controllers.

But, you can pass variables from an emailtemplate or Apex Page into a controller. So I create one conmponent with a list of input varaibles (attributes) all of the styling and code is held here:

<apex:attribute name="FirstName"   type="String" description="" />
<apex:attribute name="Receipt_Text"   type="String" description="" />
<apex:attribute name="Mailing_Address_Formula"   type="String" description="" />
<apex:attribute name="Receipt_Name"   type="String" description="" />

So within my email, I have:

<messaging:attachment renderAs="pdf" filename="Your Donation Receipt -     {!relatedTo.Date_Received_in_Bank__c}">

Mailing_Address_Formula="{!if(  len(relatedTo.Account.Preferred_Mailing_Address_Formula__c) >9 , relatedTo.Account.Receipt_Name_Formula__c,"") }"
FirstName="{!relatedTo.npsp__Primary_Contact__r.FirstName }"   


Here is my visual-force page to allow viewing the receipt in the brow

<apex:page renderAs="pdf"  standardController="Opportunity" showHeader="false" Sidebar="false"  applyHtmlTag="false" applyBodyTag="false"  >

<c:ReceiptCode Mailing_Address_Formula="{!if(      len(Opportunity.Account.Preferred_Mailing_Address_Formula__c) >9 , Opportunity.Account.Receipt_Name_Formula__c,"") }"
    FirstName="{!Opportunity.npsp__Primary_Contact__r.FirstName }"    




There is a bit of duplication in specifying the values. But, I think this is a good thing as I could change the values in the receipt dynamically on a per email basis.


As per the description of Apex:include , We can't pass the id to the page. But we can do the same in components. Just reflect the page into component and get it utilized in email template.

<messaging:attachment filename="myContent.pdf" renderAs="pdf">
<c:myComponentToGeneratePDF position="1" id="{!relatedTo.Id}"/>
  • 1
    Thank you for your response. Controllers don't have standard controllers. So it makes this less flexible as each org needs to modify the apex controller in their sandbox rather than just tweak the visual force Jul 24, 2017 at 20:21

Assuming you want the page to be rendered in html in the email body and the attachment to be the body in pdf format, I have done the following.

You can create 2 components, one rendered as pdf and the other not rendered as pdf and add both to the email template.

Or you can add the code in the component and put in your VF Email template, minus the first and last tags for the component.

I would do the later so i can just edit the body of the email in the email template instead of the component. Components are usually to be re-used. Either one should work. The VF email template has a related object and it has the functionality of a standard controller, so there should be no issue there.

If you add more of your code, we can be more specific.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .