You need to sign in to do that
Don't have an account?
Email with PDF attachment - Error Opening the PDF
Hi All,
I need to send an email with a PDF attachment. I have developed a normal visualforce page (didn't render as PDF). And then using apex code, am getting the body of it, and sending it as PDF attachment.
I am receving the email with the PDF attachment, but when I try to open the PDF, it isn't opening. It gives the following decode error - "Adobe reader cannot open the PDF because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded)"
Below is the Apex class that is taking the body of vf page and making it a PDF attachment. Please let me know if I am missing anything. Thanks.
global class CustomersPDF_SendEmail implements Schedulable { global void execute(SchedulableContext sc) { PageReference pdf = Page.CustomersPDF; pdf.getParameters().put('id','001O000000ECvg4'); Blob body; try { body = pdf.getContent(); } catch (VisualforceException e) { body = Blob.valueOf('Some Text'); } Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment(); attach.setContentType('application/pdf'); attach.setFileName('KeyCustomers.pdf'); attach.setInline(false); attach.Body = body; Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setUseSignature(false); mail.setToAddresses(new String[] {'abc@gmail.com'}); mail.setSubject('Customers PDF - Demo'); mail.setHtmlBody('Check the attachment!'); mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach }); // Send the email Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } }
Easy. Inside the email template write:
<messaging:attachment renderAs="PDF" filename="myPDFFile.pdf">
<html>
<body>
<!-- Here should have all the data that should be in the PDF.....
can be component that generate all the data -->
</body>
</html>
</messaging:attachment>
All Answers
body = pdf.getContentaspdf();
I am not getting the error when I open BUT the PDF attachment is just plain blank! No information in it.
I tried with both VF page renderedas="PDF" and without it also. Same result both times.
Any thoughts?
Here's the VF Code. It has just 2 dataTables
Also, let you know, when I render this as PDF and check, it is working fine.
Only when I send this VF via email as PDF attachment using the apex code that I pasted earlier, it is opening as BLANK PDF.
Ok Now this is not great.
I commented everything in the VF. And added this - <b>Hello World</b>
Even then it is the same error. So this issue is not related to VF?
1) The adobe error message.
When I used body = pdf.getContent();
http://i41.tinypic.com/2q0oebp.jpg
2) The blank page.
When I used body = pdf.getContentasPDF();
http://i43.tinypic.com/246qcfo.jpg
The page looks fine to me..
Ok Thanks.
Here's the apex class
Try to load the page in the URL with same parameters,
to check if the problem is in the page or in the PDF.
Hi,
Maybe this is the issue.
Found in SF documentation.
getContent
getContentAsPDF
This method can't be used in:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_pages_pagereference.htm
You can create Email template with component that include all the data (as in your current page).
Then I think in the code you can set the email field: templateId, to the ID of your template.
The VF page is now embeded inside the email body. How can I make it as an attachment?
Easy. Inside the email template write:
<messaging:attachment renderAs="PDF" filename="myPDFFile.pdf">
<html>
<body>
<!-- Here should have all the data that should be in the PDF.....
can be component that generate all the data -->
</body>
</html>
</messaging:attachment>
Thanks very much Liron169. Worked perfect.
Here's the complete code for others to refer.
The following code works fine:
public class GenerateEmail
{
public static void sendEmail()
{
List<Messaging.EmailFileAttachment> emailAttachList=new List<Messaging.EmailFileAttachment>();
Pagereference ref = page.YourPageName;
ref.setRedirect(true);
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
Messaging.EmailFileAttachment fileAttachment = new Messaging.EmailFileAttachment();
Blob b;
try
{
b = ref.getContentAsPDF();
}
catch (VisualforceException e)
{
System.debug(e.getMessage());
b = Blob.valueOf('No content in PDF');
}
fileAttachment.setBody(b);
List<ID> EmailList = new List<ID>();
List <Contact> contactList = new List <Contact>([Select Id, Email from contact where id = 'xxxxxxxxxx']); /*change this because ID's differ from org to org, this was just for testing*/
EmailTemplate template = [SELECT Id, Name FROM EmailTemplate where name = 'OptionalEmailTemplate' LIMIT 1];
for(Contact c : contactList)
{
EmailList.add(c.id);
}
// Create the email attachment
fileAttachment.setFileName('YourFileName'+ date.today().day() + '_' + date.today().month() + '_' + date.today().year() + '.pdf');
fileAttachment.setBody(b);
emailAttachList.add(fileAttachment);
if(emailAttachList!=null)
{
email.setFileAttachments(emailAttachList);
}
email.setToAddresses(EmailList);
email.setSubject('Test PDF Email');
email.setPlainTextBody('This is a test email.');
email.setTemplateId(template.id);
try
{
Messaging.sendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
}
catch(exception e)
{
System.debug(e.getMessage());
}
}
}