I have a Visualforce page that renders perfectly as a PDF that I'd like to attach to an opportunity record and to attach to an email.

Whenever the code gets to getContent(), I keep getting an error saying

Too many nested getContent calls

Below, you can see the code is very simple. Just pulls data to fit into the VF page in the substitution blanks then tries to create the PDF. Looking at the debug log, the call to getContent() only happens once.

I'm stumped!

Thanks for your ideas...

public with sharing class GenerateThankYouLetterAsPDF {
  // a handful of fields that are {!field} substitutions on the VF page
    public id oppID {get; set;}
    public string giftAmount {set; get;}
    public string giftDate {set; get;}
    //  etc... 

    public GenerateThankYouLetterAsPDF() {
        id oppID = id.valueof( ApexPages.currentPage().getParameters().get('id') ); 

            // set the values of field substituions 

        string filename = 'donation.pdf'; 
        // Generate the PDF 
        PageReference pdf = ApexPages.currentPage();
        system.debug ('calling getContent...');
        blob b = pdf.getContent();

3 Answers 3


Since you are using getContent() method in the constructor and calling the same page with same controller again from constructor, it causes recursive execution.

You will have to change the creation of blob - blob b = pdf.getContent(); or the code to generate PDF to some other method other than constructor.

  • you were exactly right. Thank you. Moving it out of the constructor solved that problem...however, I think there's still some recursion because it's now generating 5 PDFs with each call. That's a problem to solve tomorrow.
    – sberley
    Jul 14, 2015 at 5:49

I presume what is happening is that the getContent call results in a request to the same page - the ApexPages.currentPage() - and so creates another controller instance that does the same getContent call and so on.

If you put another system.debug before the getContent call I think you will see that pattern. Create a separate page and controller or pass a flag to avoid the cycle.

public class pdfAttachment{
public String orderId = '';
public pdfAttachment(ApexPages.standardController controller){

public pageReference attachment(){
    orderId = ApexPages.currentPage().getParameters().get('id');
    PageReference attachment = Page.pdfAttachment2;

    Attachment e = new Attachment();

    Blob body;

        body = attachment.getContentAsPDF();

    catch(Exception t){
        body = Blob.valueOf('Cannot create PDF');

    e.body = body;
    e.Name = 'Attachment - '+ system.Now();
    e.isPrivate = false;
    e.parentId = orderId;
    e.ContentType = 'application/pdf';
    insert e;

    return new PageReference('/' + oppId);
  • Visualforce Page 1<apex:page standardController="Order" extensions="pdfAttachment" action="{!attachment}"> </apex:page> Jan 29, 2017 at 8:48
  • Visualforce Page 2 <apex:page standardController="Order" extensions="pdfAttachment"> <apex:form > <apex:pageBlock > <apex:pageBlockSection > <apex:outputField value="{!Order.AccountId}"/> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page> Jan 29, 2017 at 8:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .