[SalesForce] Attaching a visualforce pdf page inside an email

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" />
</messaging:attachment>

Best Answer

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}">

<c:ReceiptCode
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 }"   
Receipt_Name="{!relatedTo.Account.Receipt_Name_Formula__c}"   
Letter_From_Name="test"  
Letter_From_Title="test"   
Amount_received="{!relatedTo.Amount_Text_for_receipt__c}"
Date_Received="{!relatedTo.Date_Received_in_Bank__c}" 

 />
</messaging:attachment>

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 }"    
    Receipt_Name="{!Opportunity.Account.Receipt_Name_Formula__c}"   
    Letter_From_Name="test"  
    Letter_From_Title="test"   
    Amount_received="{!Opportunity.Amount_Text_for_receipt__c}"
    Date_Received="{!Opportunity.Date_Received_in_Bank__c}" 

    Receipt_Text="{!Opportunity.Campaign.Receipt_Text__c}" 

 />

</apex:page>

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.

Related Topic