+ Start a Discussion
B2000B2000 

Save Attachment saves the Current Page and not the renderedAs PDF VF Page

I am trying to save a renderedAs PDF VF page as an attachment.  Instead of saving the PDF page, it is saving the current page as the attachment.  The current page is: pTestEmail.  The button in the pTestEmail is used to save pTestReport (renderedAs PDF) as an attachment to the account. pTestReport has a component embedded in the VF page (this is a requirement).  The contents of pTestEmail get saved as the attachment: MAIN PAGE (Email Button) and not the pTestReport: PDF COMPONENT
Controller: cTestEmailReport
public with sharing class cTestEmailReport 
{	
   public	Account	a {get;set;}
   public String reportName = ApexPages.currentPage().getParameters().get('reportName');
    
    public cTestEmailReport(ApexPages.StandardController stdCon) 
    {
        this.a = (Account)stdCon.getRecord();       
        a = [Select Id, Name from Account where id = :a.id];        
    }
    
    public pageReference sendEmail()
    {
	PageReference pr = new PageReference('/apex/pTestReport');
    	pr.getParameters().put('id',a.id);
	pr.setRedirect(true);
	system.debug(pr);		
	Attachment att = new Attachment(Name=reportName,Body=pr.getContent(),ParentId=a.id);
	insert att;
 	pr = new PageReference ('/'+a.id);
 	pr.setRedirect(true);
	return pr;		
    }
 
}

VF Page: pTestEmail
<apex:page standardController="Account" extensions="cTestEmailReport">
<apex:form id="form">
MAIN PAGE
<apex:commandbutton value="EMAIL" action="{!sendEmail}" rerender="form"/>
</apex:form>
</apex:page>

VF Page: pTestReport
<apex:page standardController="Account" showHeader="false"  sidebar="false" renderAs="PDF" standardstylesheets="false" applyBodyTag="false">
    <c:compTest />  
</apex:page>

Component: compTest
<apex:component >PDF COMPONENT</apex:component>

 
ShashankShashank (Salesforce Developers) 
Please see if this helps: http://www.sundoginteractive.com/sunblog/posts/a-recipe-for-saving-a-pdf-as-an-attachment-salesforce
Benjamin KassBenjamin Kass
I had the same issue. For me, removing the 'rerender' tag from the commandbutton fixed the problem. I am not sure why this would be the case.
<apex:commandbutton value="EMAIL" action="{!sendEmail}"/>
instead of 
<apex:commandbutton value="EMAIL" action="{!sendEmail}" rerender="form"/>