I am trying to add pdf from static resources to my Visual force email template attachment tag. VF email template which will be used by a workflow.I was able to render a image file from static resource as pdf, but how do I get an existing pdf as pdf attachment in email template. apex:page tag are not vaild in email template. Also I tried reading he pdf body in a controller but it is not in readable format.

Please let me know how do I attach the pdf from static resources in visual force email template


Here is a working solution. First I read the static resource PDF in a blob and attached that blob with the email. I hope this will help.

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setSubject('Email using Apex');
String[] toAddresses = new String[] {'xxxxxxxx@gmail.com'};
email.setHtmlBody('<html><body>Hi <b>Saroj</b></body></html>');
StaticResource sr = [Select  s.Name, s.Id, s.Body From StaticResource s where s.Name = 'static_resource']; // 'static_resource' is the name of the static resource PDF.
Blob tempBlob = sr.Body;
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
Messaging.SingleEmailMessage[] emailList = new Messaging.SingleEmailMessage[] {email};

You mentioned that you already success render PDF page, so I assume you have Visualforce that render as PDF.In this code I named it as PDF_VF.So I attached the VisualForce page into Messaging.EmailFileAttachment.

 PageReference pdfPlan=Page.PDF_VF;
Blob bPlan= pdfPlan.getContent();
 Messaging.EmailFileAttachment[] attachments= new Messaging.EmailFileAttachment[] {};
            Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
  • Sorry but I didnot get what you said, previouly I had tried send a image from static resource as pdf attachment. But now I want to send a existing pdf as pdf attachment through my vf email template. – user6886 Jul 10 '15 at 6:09
  • <messaging:attachment renderAs="pdf" filename="Testsample.pdf"> <html> <body > <apex:outputText value="{!$Resource.mypdf}"/> <apex:outputPanel layout="block" rendered="{!$Resource.mypdf}"> {!$Resource.mypdf} </apex:outputPanel> <apex:sectionHeader title="test" subtitle="test1" help="{!$Resource.mypdf}" /> </body> </html> </messaging:attachment> – user6886 Jul 10 '15 at 6:11
  • Given above is my attachment tag in VF email template. but I only get the partial link of pdf in the attachment. – user6886 Jul 10 '15 at 6:12
  • You can paste your code in your question for better view.Surround your code with {}. – unidha Jul 10 '15 at 6:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.