function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nisha VishwasraoNisha Vishwasrao 

getContentAsPDF() returning blank PDF

I am facing the issue where PDF getting generated from VF page are blank. 
I do not have the corresponding critical update activated to treat getContentAsPDF() Methods as Callouts. 
The method getContentAsPDF() is invoked from a method SendPDF on custom controller class. And this SendPDF method is getting called as an action from a VF page.
Below is the code of this method. After upsert Survey, trigger class gets invoked on Survey object.

public static void SendPDF()
{
String AccountId = ApexPages.currentPage().getParameters().get('id');
String SurveyId = ApexPages.currentPage().getParameters().get('survey');

Survey__c[] Survey = [ SELECT Id, Name, Status__c, Send_PDF__c
FROM Survey__c
WHERE Id = :SurveyId LIMIT 1 ];

if (Survey[0].Status__c.equals('Complete') && 
(Survey[0].Send_PDF__c == null || ! Survey[0].Send_PDF__c.equals('Sent')))
{
PageReference pdfReport = Page.SurveyPg;

pdfReport.getParameters().put('id', AccountId);
pdfReport.getParameters().put('survey', SurveyId );
//System.debug(pdfReport);

Blob PDFBlob = pdfReport.getcontentAsPdf();
String PDFName = 'Survey' + Survey[0].Name + '.pdf';

Attachment attachment = new Attachment();
attachment.Body = PDFBlob;
attachment.Name = PDFName;
attachment.ParentId = SurveyId;
insert attachment;

Survey[0].Send_PDF__c = 'Sent';
upsert Survey;
}
}

We are not sending the PDF as email but instead attaching it to the survey record.
I am not directly calling the method from trigger and also do not have the critical update activated but still facing the issue.
Is there something else I need to do to make sure that getContentAsPDF() works correctly?
Harish RamachandruniHarish Ramachandruni
Hi ,


can you check  pgf page in brouser and debug . Add Bellow code if  pdf page working proper  with passing account & suryid .
 
public static void SendPDF()
{
String AccountId = ApexPages.currentPage().getParameters().get('id');
String SurveyId = ApexPages.currentPage().getParameters().get('survey');

Survey__c[] Survey = [ SELECT Id, Name, Status__c, Send_PDF__c
FROM Survey__c
WHERE Id = :SurveyId LIMIT 1 ];


PageReference pdf = Page. SurveyPg;
    // add parent id to the parameters for standardcontroller
    pdf.getParameters().put('id',accountId);
    pdf. getParameters().put('survey', SurveyId );


    // the contents of the attachment from the pdf
    Blob body;

    try {

      // returns the output of the page as a PDF
      body = pdf.getContent();

    // need to pass unit test -- current bug  
    } catch (VisualforceException e) {
      body = Blob.valueOf('Some Text');
    }

    Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
    attach.setContentType('application/pdf');
    attach.setFileName('testPdf.pdf');
    attach.setInline(false);
    attach.Body = body;

new String[]  email  = {‘harish.rao.salesforce@gmail.com’,’test@gmail.com’}

    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setUseSignature(false);
    mail.setToAddresses(email  );
    mail.setSubject('PDF Email Demo');
    mail.setHtmlBody('Here is the email you requested! Check the attachment!');
    mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach }); 

    // Send the email
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Email with PDF sent to '+email));


Survey[0].Send_PDF__c = 'Sent';
upsert Survey;
}
}




Regards ,
Harish.R.

 
Rob GoodmanRob Goodman
I've had a similar problem and found that disabling Development Mode on my User record made the difference.