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?
3 Answers
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.
</messaging:attachment>
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
-
-
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.– VictorCommented Mar 21, 2015 at 21:14
-
1I've asked because it didn't work :) Even encoded blob is not rendered properly. Commented Mar 22, 2015 at 11:37
-
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:
- We had a Visualforce page, rendered as PDF and the content on it was dynamic based on its controller.
- 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.... Commented 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:
- 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;
- 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.
Apex
can get around limits and still send as long as it is to aUser
orContact
. Does not count against any limit.