I have an api that gets pdf invoices and stores them as Salesforce Files. I then want the sales rep to be able to send the email template with the pdf attached. Using standard visualforce email templates is preferable so they can see what they are sending and admins can update the templates easily.

I've tried getting the ContentVersion.versionData as a blob and adding it to a component. I tried it as a string with Base64Encode.

Controller to get File

File = [select id, ContentDocumentId,title,pathOnClient,versionData,FileType from ContentVersion 
                           title like 'Invoice%'and 
                           //pathOnClient = :recordId and 
                           IsLatest = true
                       ORDER BY lastmodifieddate desc 
                       limit 1];

Email Template

<messaging:emailTemplate subject="Here is your invoice" recipientType="Contact" relatedToType="Opportunity" >

<messaging:attachment renderAs="pdf" filename="{!relatedTo.InvoiceNumber__c +'.pdf'}">
<c:LatestInvoice />

Within the attached pdf is a blob I get this message


When I encode the blob to a string I get:

  • That's expected. You're trying to output binary data (a blob) into Visualforce, so a "toString" implementation is called, which causes a string similar to what you've seen here. I don't believe there's a way to include a binary file in a Visualforce template right now. messaging:attachment allows you to render an attachment directly, such as in HTML/PDF, CSV, etc. – sfdcfox Oct 23 '17 at 0:52
  • I thought that might be the case, annoying. Can you convert the pdf to HTML and rerender? – Daniel Howell Oct 23 '17 at 1:36
  • Not within Salesforce. Do you have an option of getting the file in HTML instead of PDF from the source provider? Or maybe you could use Heroku to translate the files from PDF to HTML, then you could include the HTML. I'm not sure you we can give a full answer in a simple answer. – sfdcfox Oct 23 '17 at 1:45
  • Thanks @sfdcfox, I think I will have to send out a content link to the file. That is what SF would want me to do. It's just not a brilliant user interface for them to download it from. – Daniel Howell Oct 23 '17 at 3:52

Your Answer

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

Browse other questions tagged or ask your own question.