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

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


  • 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.


  1. Now if the standard controller is removed from Page 1 it seems be happy and PDF is generated properly
  2. Some how if you add a Extension to Page 2(the PDF page) it seems to be happy again and pdf is generated properly
  3. 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

  • Just a suggestion on debugging this yourself in the meantime. If you remove the renderAs="PDF" and try it again, at least then you can likely see what might be the cause? – Andrew Fawcett Feb 21 '14 at 18:26
  • @AndrewFawcett i tried doing that the VF page renders properly and but the PDF doesn't and surprisingly if you just a customcontroller it starts working – Avidev9 Feb 21 '14 at 20:30
  • Ah ok, definitely sounds like a bug then. – Andrew Fawcett Feb 22 '14 at 8:05

I ran into this recently. I don't know if it is a bug or not, but it is annoying! Basically if your two pages share the same controllers, then it ignores the new page. If you do TestPDF.getContentasPDF(), you'll see that it is rendering Page 1 instead of Page 2.

If you take the oncomplete and the rerender off the CommandButton, it will render the correct page.

| improve this answer | |
  • well by the way I am observing that if I am using a standard controller with an extension and PDF page doesnt have a extension, the PDF generated is corrupted. Well seems like a platform bug. – Avidev9 Jan 2 '14 at 8:38
  • Yes, if Page 2 has the same controllers or at least doesn't add add any additional ones, then it doesn't seem to change the page. – Daniel Hoechst Jan 2 '14 at 16:26
  • well I am going to raise a case with SF support, not sure if I can convince them that it is a platform bug. – Avidev9 Jan 2 '14 at 18:44
  • Daniel Hoechst, sskular. Thanks a lot! I was really desperate searching about this issue, but my problem it was the rerender on the command button when I send the email. As you say, the file is corrupted because the rerender doesn't permitted the creation of PDF file. – Marco Zp Mar 8 '16 at 19:54

The answer from Daniel pointed that if you remove the oncomplete and rerender attributes from the button that initiates the generation of the PDF, it will magically start working - it will switch to the correct page, generate a PDF, and the generated PDF will render perfectly.

It is worth exploring other options for exectuting logic on oncomplete, at least until the bug is resolved.

So, as a temporary workaround, try removing the oncomplete and rerender attributes on the initiating button.

EDIT: For example, I created a second VF page (without a controller) that the extension controller method redirected to after attaching the PDF, and on that page I had all logic in JavaScript that should have been called by oncomplete.

Hope this helps Cheers

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.