+ Start a Discussion

How to add a standard page content in mail attachment



So far I can get a visualforce page content as pdf by pageRef.getContentAsPdf() method.


Now I want to get a standard page content and send them as pdf attachment in my mail.

I use the following code:

Audit__c audit = new Audit__c();
audit.Id = auditId;
PageReference pageRef = new ApexPages.StandardController(audit).view();
pageRef.s  etRedirect(true);

Blob b = pageRef.getContent();

Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();

But when I try to open this pdf file I get some error as "wasn't correctly decoded".


If I use pageRef.getContentAsPdf(), the code can't go through and doesn't have any error info.


So does anyone know how to change a standard record view page as a pdf file and send it as attachment?


Thank you!!!



Using getContent() returns HTML, not a PDF, so of course it wouldn't work.

Using getContentAsPDF() should work... I just tried this, and I received an Internal Salesforce Error. Which means that it should work, except there's some bug with the platform.

I don't think there's any way you can fix this at the moment.