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
ArmanMArmanM 

Save PDF as attachment and option to send the PDF in email

Hi, 

I have created an Visualforce page that generates an PDF of a opportunity on a click of a button in opportunity layout. Now I want to be able to save that PDF as an attachment in opportunity and also have an option to send that PDF as an email attachment within salesforce. Here is my VF page -
 
<apex:page standardController="Opportunity" renderAs="pdf" applyBodyTag="false" > 
    <html> 
        <apex:form >
        <apex:pageBlock >
        <center>
         <table width="100%" style="font-family: Arial, Helvetica, sans-serif; border-collapse: collapse;" cellpadding="5">
        
        <head>
       
            <tr>
                <td colspan="4" >
                    <img src="some img" ></img> 
                </td>
            </tr> 
        </head>
       
        <body>
           
            <tr>
                <td colspan="4" style="font-size: x-large; font-weight: bold">
                    {!Opportunity.Account.Name}
                </td>
            </tr>
          
            <tr>
                <td colspan="4"> </td>
            </tr>
            <tr>
                <td colspan="4" style="font-weight: bold; border-bottom-style: solid; border-bottom-width: thick;
                    border-bottom-color: #30b55a;">
                    Campaign Information </td>
            </tr>
              
                    <tr>
                            <td align="left" style="color: #808080; border-right-style: solid; border-right-width: thin;
                    border-right-color: #30b55a;">
                                Campaign Name </td>
                            <td align="left" style="padding-left: 5px;"> 
                                {!Opportunity.Name} </td>
                                
                        </tr>
                   <tr>                    
                            <td align="left" style="color: #808080;border-right-style: solid; border-right-width: thin;
                    border-right-color: #30b55a;">
                                Brand </td>
                            <td align="left" style="padding-left: 5px;">
                                {!Opportunity.Account.Name} </td>
                        </tr>
                       
            <tr>
                <td align="left" style="color: #808080; border-right-style: solid; border-right-width: thin;
                    border-right-color: #30b55a;">
                    Start Date
                </td>
                    <td style="font-weight: normal; padding-left: 5px;">
                        <apex:outputText value="{0,date,MM'/'dd'/'yyyy}">
                        <apex:param value="{!Opportunity.Campaign_Start_Date__c}" /> 
                        </apex:outputText>
                    </td>
            </tr>
            
            <tr>
                <td align="left" style="color: #808080; border-right-style: solid; border-right-width: thin;
                    border-right-color: #30b55a;">
                    End Date
                </td>
                    <td style="font-weight: normal; padding-left: 5px;">
                        <apex:outputText value="{0,date,MM'/'dd'/'yyyy}">
                            <apex:param value="{!Opportunity.Campaign_End_Date__c}" /> 
                        </apex:outputText>
                    </td>
            </tr>
            
           
             <tr>
                <td align="left" style="color: #808080; border-right-style: solid; border-right-width: thin;
                    border-right-color: #30b55a;">
                    Duration 
                </td>
                    <td style="font-weight: normal; padding-left: 5px;">
                        <apex:outputText value="{0,number, }">
                            <apex:param value="{!Opportunity.Campaign_Duration__c}"/>
                        </apex:outputText> 
                    </td>
            </tr>
            <tr>
                <td align="left" style="color: #808080; border-right-style: solid; border-right-width: thin;
                    border-right-color: #30b55a;">
                    Geo
                </td>
                    <td style="font-weight: normal; padding-left: 5px;">
                        {!Opportunity.Targeting_Country__c} 
                    </td> 
            </tr>
            
             <tr>
                <td colspan="4">
                    &nbsp;
                </td>
            </tr>
            
        <tr>    
            <td colspan="4" style="font-weight: bold; border-bottom-style: solid; border-bottom-width: thick;
                border-bottom-color: #30b55a;">
                Mobile Spots -
                
                <apex:outputText value="{0,number,000,000}">
                    <apex:param value="{!Opportunity.Mobile_Spots_B__c}"/>
                </apex:outputText> 
            </td>
        </tr>
        <tr>
                <td align="left" style="color: #808080; border-right-style: solid; border-right-width: thin;
                    border-right-color: #30b55a;">
                    Audio Ads 
                </td>
                    <td style="font-weight: normal; padding-left: 5px;">
                        <apex:outputText value="{0, number, }">
                            <apex:param value="{!Opportunity.Mobile_Spots_B__c}"/>
                        </apex:outputText>
                    </td>
        </tr>
        <tr>
             <td width="160px" align="left" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #30b55a;">
                Companion Banner
             </td>
                <td style="font-weight: normal; padding-left: 5px;">
                <apex:outputText value="{0, number, }">
                    <apex:param value="{!Opportunity.Mobile_Companion_Banner__c}"/>
                </apex:outputText>                    
                </td>
        </tr>
        <tr>
            <td align="left" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #30b55a;">
                Display Impression
            </td>
            <td style="font-weight: normal; padding-left: 5px;">
                <apex:outputText value="{0, number, }">
                    <apex:param value="{!Opportunity.Mobile_Display_Impression__c}"/>
                </apex:outputText>
            </td>
        </tr>
        <tr>
            <td align="left" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #30b55a;">
                Interstitial
            </td>
            <td style="font-weight: normal; padding-left: 5px;">
                <apex:outputText value="{0, number, }">
                    <apex:param value="{!Opportunity.Moblie_Interstitial__c}"/>
                </apex:outputText>
            </td>
        </tr>
        
        <tr>
            <td colspan = "4">
                &nbsp;
            </td>
        </tr>
        
        <tr style="display:{!IF(OR(Opportunity.Web_Spots_B__c == NULL, Opportunity.Web_Display_Impression__c == NULL, Opportunity.Web_Interstitial__c == NULL), 'none','block')}">
            <td colspan="4" style="font-weight: bold; border-bottom-style: solid; border-bottom-width: thick;
                border-bottom-color: #30b55a;">
                Web Spots -
                    <apex:outputText value="{0, number,}">
                    <apex:param value="{!Opportunity.Web_Spots_B__c}"/>
                    </apex:outputText>
            </td>
        </tr>
        

        <tr style="display:{!IF(OR(Opportunity.Web_Spots_B__c == NULL, Opportunity.Web_Display_Impression__c == NULL, Opportunity.Web_Interstitial__c == NULL), 'none','block')}">
            <td align="left" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #30b55a;">
                Audio Ads 
            </td>
            <td style="font-weight: normal; padding-left: 5px;">
                <apex:outputText value="{0, number, }">
                    <apex:param value="{!Opportunity.Web_Spots_B__c}"/>
                </apex:outputText>
            </td>
        </tr>   
        
        
        
        <tr style="display:{!IF(OR(Opportunity.Web_Spots_B__c == NULL, Opportunity.Web_Display_Impression__c == NULL, Opportunity.Web_Interstitial__c == NULL), 'none','block')}">
            <td align="left" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #30b55a;">
                Companion Banner
            </td>
            <td style="font-weight: normal; padding-left: 5px;">
                <apex:outputText value="{0, number, }">
                <apex:param value="{!Opportunity.Web_Spots_B__c}"/> 
                </apex:outputText>
            </td>
        </tr>   
       
        
        <tr style="display:{!IF(OR(Opportunity.Web_Spots_B__c == NULL, Opportunity.Web_Display_Impression__c == NULL, Opportunity.Web_Interstitial__c == NULL), 'none','block')}">
            <td align="left" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #30b55a;">
                Display Impression
            </td>
            <td style="font-weight: normal; padding-left: 5px;">
                <apex:outputText value="{0, number,}">
                    <apex:param value="{!Opportunity.Web_Display_Impression__c}"/>
                </apex:outputText>
            </td>
      </tr> 
      
       <tr style="display:{!IF(OR(Opportunity.Web_Spots_B__c == NULL, Opportunity.Web_Display_Impression__c == NULL, Opportunity.Web_Interstitial__c == NULL), 'none','block')}">
            <td align="left" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #30b55a;">
                Interstitial
            </td>
            <td style="font-weight: normal; padding-left: 5px;">
                <apex:outputText value="{0, number,}">
                    <apex:param value="{!Opportunity.Web_Interstitial__c}"/>
                </apex:outputText>
            </td>
        </tr> 
        
       
        <tr style="display:{!IF(OR(Opportunity.Web_Spots_B__c == NULL, Opportunity.Web_Display_Impression__c == NULL, Opportunity.Web_Interstitial__c == NULL), 'none','block')}">
            <td colspan="4">
                &nbsp;
            </td>
        </tr>
        
          <tr>
            <td colspan="4" style="font-weight: bold; border-bottom-style: solid; border-bottom-width: thick;
                border-bottom-color: #30b55a;">
                Total 
                </td> 
        </tr>
        <tr>
            <td align="left" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #30b55a;">
                Spots
            </td>
            <td style="font-weight: normal; padding-left: 5px;">
                <apex:outputText value="{0, number,}">
                    <apex:param value="{!Opportunity.Total_Spot_Booked__c}"/>
                </apex:outputText>                
            </td>
        </tr>
        <tr>
            <td align="left" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #30b55a;">
                Approx. Impressions 
            </td>
            <td style="font-weight: normal; padding-left: 5px;">
                <apex:OutputText value="{0, number, }">
                    <apex:param value="{!Opportunity.Total_Impressions__c}"/> 
                </apex:OutputText>                
            </td>
        </tr>
        <tr>
            <td colspan="4">
                &nbsp;
            </td>
        </tr>
        <tr>
            <td colspan="4" style="font-size: medium; font-weight: normal">
                Note: Spots will be spread out on the Mobile Playlist, Radio and ROS.        
            </td>
        </tr>
        
        <tr>
            <td colspan="4">
                &nbsp;
            </td>
        </tr>
        
        <tr>
            <td colspan="4" style="font-size: medium; font-weight: bold">
                <font color ="blue"> Deliverables from {!Opportunity.Account.Name} </font>
            </td>
        </tr>
        <tr>
            <td colspan="4" style="font-size: medium; font-weight: normal">
                <font color ="blue"> {!Opportunity.Campaign_Comments__c} </font>
            </td>
        </tr>
        
        <tr>
            <td colspan="4">
                &nbsp;
            </td>
        </tr>
        <tr>
            <td colspan="4">
                &nbsp;
            </td>
        </tr>
        
        <tr>
            <td colspan="4">
                &nbsp;
            </td>
        </tr>
        
         <tr>
            <td colspan="4" style="font-size: medium; font-weight: bold">
        Confidentiality:                 
            </td>
        </tr>
        
        <tr>
            <td colspan="4" style="font-size: medium; font-weight: normal">
        <p> The parties involved acknowledge and agree that this term sheet, its material terms and conditions and any other information 
            
        disclosed by one party to the other party in connection with this proposal or negotiation of the agreement is Confidential Information.</p>
        
         </td>
        </tr>
        
        <tr>
            <td colspan="4" style="font-size: medium; font-weight: bold">
        
        Above terms agreed to by:
            </td>
        </tr>

        <tr>
            <td colspan="4" style="font-size: medium; font-weight: normal">
        <br></br>
        Name:                           ____________________________________________________________________________________
        <br></br> Designation:                      ____________________________________________________________________________________

        <br></br> Company Name:             ____________________________________________________________________________________ 
        
        <br></br> Email Address:                ____________________________________________________________________________________ 
        
        <br></br> Mailing Address and Contact Details: ____________________________________________________________________________________ 
        
        <br></br> Signature:                            ____________________________________________________________________________________ 
        
        <br></br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            Date
        </td>
        </tr>
        <tr>
            <td colspan="4" style="font-size: medium; font-weight: bold">
        Above terms agreed to by (Name):
            </td>
        </tr>
        
        <tr>
            <td colspan="4" style="font-size: medium; font-weight: normal">
        <br></br> Name:                             ____________________________________________________________________________________ 
        
        <br></br> Designation:                      ____________________________________________________________________________________
        
        <br></br> Signature:                            ____________________________________________________________________________________
        <br></br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            Date                                                                                                        
            </td>
        </tr>

 </body>
        </table>
        </center>
        </apex:pageBlock> 
        </apex:form>
        
    </html> 
</apex:page>

Thank You !
ArmanMArmanM
Yes, I was looking throught that but I'm confused about some points of the codes i.e Im not understanding line 24 of his controller code. "PageReference pdf = Page.TWC_Contract" - What is that doing and how would this be relevent in my case
Swaraj Behera 7Swaraj Behera 7
Please post your apex class and VF page from where you are sending mail.
Thanks.