So, some background. I have created a visualforce page that displays a Case and renders it as a PDF. This works fine. I have included it on a Case Page Layout as a Detail Button and it correctly formats all the information when clicked.
I have now created a second Visualforce page that uses the standard case controller and a new extension class. This is intended to email a recipient with the PDF page above attached. I have added it to the page layout as a detail button and in my tests or if I run it via salesforce classic it works.
However our Org uses the Service Cloud Console and I want to load this functionality from there. Obviously the page layout contains the button so I click it, I enter the email address and click send (I can see the case controller is working as the page displays the casenumber etc). On receipt of the email I always receive an unformatted/broken/blank PDF document and for the life of me I don't know why.
Can anyone shed any light as to why it would work in my tests but not in the console?
Thanks - I haven't included my code above as it seems to be working if you think it would help I can copy the code in.
EDIT: Attachment code below:
public with sharing class PDFEmailController {
private final Case cs;
public String caseID {get;set;}
public String email {get;set;}
public PDFEmailController(ApexPages.StandardController controller) {
this.cs = (Case)controller.getRecord();
}
public PageReference sendPdf() {
PageReference pdf = Page.PDFPAGE123;
// add parent id to the parameters for standardcontroller
pdf.getParameters().put('id',cs.ID);
// 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;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setUseSignature(false);
mail.setToAddresses(new String[] { email });
mail.setSubject('PDF TESTING');
mail.setHtmlBody('<p>Please see attached</p>');
mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });
// Send the email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, cs.CaseNumber + ' with PDF sent to '+email));
return null;
}
Edit - as comment, I changed it to getContentAsPDF() and this returns an almost blank PDF document (it only lists the visualforce page and controller with an x on the right hand side).
getContentAsPDF()
method may be more appropriate here...