+ Start a Discussion

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');
	Attachment att = new Attachment(Name=reportName,Body=pr.getContent(),ParentId=a.id);
	insert att;
 	pr = new PageReference ('/'+a.id);
	return pr;		

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

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

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