Well lately I noticed a weird behaviour of Pagereference getContent()
method while using it with a standard controller along with an extension.
So here is bit of code to demonstrate the issue.
PAGE 1 : Original Page to Generate PDF
<apex:page extensions="TestPage_Con" sidebar="false" standardController="Account">
<apex:form id="frm">
<apex:pageBlock id="pb">
<apex:commandButton value="Generate PDF" reRender="frm" action="{!generatePDF}" oncomplete="alert('Complete')"/>
</apex:pageBlock>
</apex:form>
public class TestPage_Con {
public TestPage_Con(ApexPages.StandardController controller) {
}
String accId = '001K000000u31wR';
public void generatePDF(){
Attachment att = new Attachment(name ='Test'+System.now()+'.pdf');
Pagereference TestPDF = Page.TestPDF;
TestPDF.getParameters().put('id',accId);
att.body = TestPDF.getContent();
att.parentid = accId;
insert att;
}
}
PAGE 2(Target PDF Page) : TestPDF
<apex:page standardController="Account" renderAs="PDF">
PASSED ACCOUNT NAME : {!Account.Name}
</apex:page>
ISSUE
- If the button "Generate Pdf" from Page 1 is used to generate the PDF the pdf which is generated and attached is corrupted and you cannot actually open it.
Observation
- Now if the standard controller is removed from Page 1 it seems be happy and PDF is generated properly
- Some how if you add a Extension to Page 2(the PDF page) it seems to be happy again and pdf is generated properly
- Using "
getContentAsPdf()
" doesn't help it infact generates the PDF content of Page 1.
Not sure if this is a Salesforce bug or I am missing something ?
Update 21-Feb-2014 Finally was able to submit the case without being redirected to a help page by the rep. Now the case stands with salesforce T3 team