function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Zhino YousefiZhino Yousefi 

Embedding an image in PDF attachment using html tags and Blob.toPdf()

I want to add an image to a pdf I am creating and attach it to an email that I send via apex code. I am using html and Blob.toPdf() to make the pdf. I saw this post regarding Blob.toPdf()'s limitations on images: https://help.salesforce.com/apex/HTViewSolution?id=000123577&language=en_US. It says:

"To work around this Blob.toPdf()'s limitation you may resort to one of the below approaches:
1. Store the image as a static resource and then use it in code. 
2. Use a Visual Force Page and render it as pdf "

So I tried both ways, the first one throws an error: "An error occurred while parsing the input string" (which happens with <img> tag no matter what src I use). 
And the second way is not an option for me cause I have to get the page content that I'm rendering as pdf and attach it to my email BUT "Page.getContent() does not work in Triggers or Email Services".

Is there a work around this limitations?
 
Best Answer chosen by Zhino Yousefi
AB TestAB Test
Hi 

  We had a similar requirement in one of our projects and find the way we followed and check if it suffices . 
  1.  We had a field on the object called Logo which was a Rich Text Area Field from which user could upload images in the record.
  2. We created a VF Page for the PDF representation on VF page and displayed that field logo using <apex:outputField />
  3. Now we got the page content as BLOB as follows 
    Blob body;
      PageReference pdf = Page.ThePageCreated;
      body=pdf.getContent();
  4. Now we created an attachment with this BLOB body and sent the mail with this attachment. 
    Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
                    attach.setContentType('application/pdf');
                    attach.setFileName('InvoicePdf.pdf');
                    attach.setInline(false);
                    attach.Body = body;

It worked for us .. Hope it helps you.

Please mark the answer as best answer or like it if it helps :)

 

All Answers

AB TestAB Test
Hi 

  We had a similar requirement in one of our projects and find the way we followed and check if it suffices . 
  1.  We had a field on the object called Logo which was a Rich Text Area Field from which user could upload images in the record.
  2. We created a VF Page for the PDF representation on VF page and displayed that field logo using <apex:outputField />
  3. Now we got the page content as BLOB as follows 
    Blob body;
      PageReference pdf = Page.ThePageCreated;
      body=pdf.getContent();
  4. Now we created an attachment with this BLOB body and sent the mail with this attachment. 
    Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
                    attach.setContentType('application/pdf');
                    attach.setFileName('InvoicePdf.pdf');
                    attach.setInline(false);
                    attach.Body = body;

It worked for us .. Hope it helps you.

Please mark the answer as best answer or like it if it helps :)

 
This was selected as the best answer
Zhino YousefiZhino Yousefi
Thanks a lot AB, It worked.
Hemant Gulati 4Hemant Gulati 4
Hi AB,

Please help me in this, i also have a similar situation, a small change that i need to store that as an attachment to a record.

From 1st VF page controller:-

A record has been saved with a rich text field includes text and images.

PageReference pageDoc = Page.pdfDoc;
pageDoc.getParameters().put('id','00B4B000000YpYb');

//to get 2nd page content
Blob body;
body=pageDoc.getContent();

//attch file to a record
Attachment attachmentFile  = new Attachment();
attachmentFile.parentId = parentId;
attachmentFile.Name =  'abc.pdf';
attachmentFile.body =body;
insert attachmentFile;


On 2nd Page VF page (pdfDoc VF page):-

<apex:page renderAs="pdf" standardController="Object__c">
    <apex:outputField value="{!Object__c.RichTextField__c}"/>
</apex:page>

Rich text field includes text and images.


But i am unable to save it as an attachment , as i got the following error:-  "common.apex.runtime.impl.ExecutionException: Unable to retrieve object".


Please help me on this.