1

We have an API which return us a base64 format of am image file. We have to attach this image file as PDF in an email and send it on.

The issue is if we use Base64.toPdf('base64 version of image file') it corrupts the file and shows Failed to load pdf.

Has anyone faced this issue, or how can I attach base64 image as PDF in an email attachment.

Update 1 I tried with a VF page inbetween,

But when I put renderAs="pdf" it fails to display image, but if I remove that tab it works fine:

<apex:page id="ImageToPDF" lightningStylesheets="true" showHeader="false" applyBodyTag="false" docType="html-5.0" controller="Test">

    <html>
        <body>
            <img src="data:image/png;base64,{!image}"/>
        </body>
    </html>
</apex:page>

Any one faced this issue to display image as pdf using renderAs

UPDATE (SOLUTION) Working Solution with Apex : https://salesforce.stackexchange.com/a/303567/45062

0

2 Answers 2

0

You're correct that you can't directly turn an image into a pdf with the Blob.toPdf() method. That method only accepts Strings as an input type. I think you're also on the right track by creating an visualforce page which renders as pdf. Unfortunately, according to this answer, it isn't possible to insert base64 as part of the data uri scheme in a visualforce page.

I think this will leave you with at least two options. You could turn the image into a pdf outside of the Salesforce platform. There are plenty of API's for that. You could also run your own service if you prefer on for instance the Heroku platform.

Alternatively, you could create a temporary attachment or file record. You can then use that record in your visualforce page to display the image. Afterwards you could delete the record again if you want to.

5
  • Heroku platform approach wont be feasible for client as they will have to pay extra, but for the second approach, I dont want it to display in VF page, I would like to send it as an email as pdf attachment (This is part where I am stuck, and as you said, I also think there is no way to do this, orelse clients will have to use image files only) Apr 21, 2020 at 11:57
  • I think you could actually do this, but i'm not a 100% sure. I think if you save the base64 as a file, you can then acces that file through the normal url for it. If you render that in a pdf I think it should be included correctly. (The url for a file is: /sfc/servlet.shepherd/version/download/<CONTENT VERSION ID starting with 068>
    – Kasper
    Apr 21, 2020 at 12:22
  • Alternatively, you could use the metadata api to create a static resource and include that in your pdf. I know for sure that images will load from a static resource in a pdf. But that sounds kinda sketchy: salesforce.stackexchange.com/questions/88182/…
    – Kasper
    Apr 21, 2020 at 12:24
  • I have done it via Apex and an intermediate page: salesforce.stackexchange.com/a/303567/45062 Apr 23, 2020 at 12:25
  • @NagendraSingh Glad you sovled it, looks like a nice solution.
    – Kasper
    Apr 23, 2020 at 12:39
-1

Save that image as Pdf ContentType. Maybe this will work

String jsonStr = '<Base64 String>';
Attachment attach = new Attachment();
attach.contentType = 'application/pdf';
attach.name = 'myfile.pdf';
attach.parentId = '0010H00002DW2Ds';
attach.body = EncodingUtil.base64Decode(jsonStr);
insert attach;
6
  • No it doesnt work, did it work for you? Apr 21, 2020 at 9:07
  • yes i have used that one Apr 21, 2020 at 9:09
  • Can you share code snippet, my base64 is an image, your might be a pdf already , that us why it would have worked. Apr 21, 2020 at 9:11
  • This does not work if base64 string is already an image. Apr 21, 2020 at 10:41
  • Nagendra, this will work, you need to create a temporary attachment. Try sending the email as future or queueable job! Apr 22, 2020 at 7:13

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.