This time I would like to ask if anyone was able to add existing document/attachment(PDF) to visualforce template as file attachment? Of course I mean add as part of logic in template(component). This file must be picked dynamically(there is query for this) but the problem is: How add it as attachment? Did any of you implemented something like this?

  • How are the templates being used? If you are sending them out from Apex, it would be fairly straightforward to convert attachments to be ready for email. I don't think it is possible from within the template, though...
    – Adrian Larson
    Mar 17, 2015 at 17:12
  • The idea is to have "smart template", workflow should send it - because of limits Mar 18, 2015 at 9:44
  • Apex can get around limits and still send as long as it is to a User or Contact. Does not count against any limit.
    – Adrian Larson
    Mar 18, 2015 at 14:03

3 Answers 3


In Visualforce email template you can use below tag to render content into a PDF attachment in final email.

<messaging:attachment renderAs="PDF" filename="fileName.pdf">
    email attachment content goes here.

But if you are trying to render an already existing PDF document from say "Documents" in salesforce, you can try below steps

1) Get the content of the document by querying document. 2) Assign this document body to a property(String type) in the controller of visualforce email template. Then use the property inside above mentioned tags.

Generally if you want more control over this kind of scenario, we go for an apex trigger and from trigger we send apex generated email. In this case you can query documents or any other object or even multiple objects and control attachment contents more precisely.

Please try below code to encode the Document body as string suitable for email attachment.It worked for me while reading text Document into apex and rendering as PDF in email,

Public class pdfContentController {
public String pdfBody{get;set;}
    public pdfContentController(){
        Document doc = [select body from document where Id = '01590000008P7xs'];
        pdfBody = EncodingUtil.urlEncode(doc.body.toString(), 'UTF-8');


It looks like, if we are reading a PDF document into string, it may not render property because of encryption. But there are some work arounds, please check below, https://developer.salesforce.com/forums/ForumsMain?id=906F00000009DKQIA2

  • Did you tried this? Mar 21, 2015 at 17:50
  • I have sent attachments from triggers using email feature in trigger. But rendering existing document as PDF attachment, I haven't tried. I don't see a reason for it to not work. Please try once. Also remember that, if you are querying document content, you should be careful regarding heap limit(6MB for regular apex transactions) and (12MB for asynchronous apex transactions). If document size is exceeding this, it will fail.
    – Victor
    Mar 21, 2015 at 21:14
  • 1
    I've asked because it didn't work :) Even encoded blob is not rendered properly. Mar 22, 2015 at 11:37
  • I have updated answer with some code I tried.
    – Victor
    Mar 24, 2015 at 6:19

Well, attaching a pdf file dynamically is possible or not I am not too sure. We did something on this line, but our approach was:

  1. We had a Visualforce page, rendered as PDF and the content on it was dynamic based on its controller.
  2. This PDF page was used as our attachment for a visualforce email template.

So this way, we managed to have dynamic content in the pdf attachment on the email. Does this help?

  • Yes, but this is still just creating. You see it is easy to include html content as attachment, the real problem is that pdf content/body is actual blob.... Mar 18, 2015 at 9:48

This seem to be not very easy to handle and potentially not possible. Instead I can give two options:

  1. Use a link to the file attached to chatter post:

ContentDocument doc = [SELECT Id FROM ContentDocument WHERE Title = 'myfile'];
String fullFileURL = URL.getSalesforceBaseUrl().toExternalForm() + '/' + doc.id;

  1. Use an AppExchange solution such as S-Drive so that you can generate links in your component to any sized document and append it to your template on the fly.

Hope this helps.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .