0

I've tried some searching and it's unclear to me if what I want to do is possible in Salesforce. I have an image file attached to a Salesforce Case, and I want to use an <apex:page showHeader="false" renderAs="pdf"> to build a PDF with that file embedded in it.

The image is a drawn signature, to produce a signed PDF, so this image is confidential.

I've tried an image that looks something like:

<apex:image url="{!imageURL}"></apex:image>

Where the imageUrl in my Aura controller is something like


    public String imageURL{
        get {
            List<ContentVersion> vers = [SELECT Id, ContentDocumentId FROM ContentVersion ORDER BY CreatedDate DESC];
            ContentVersion v = vers.get(0);
            return '/servlet/servlet.FileDownload?file=' + v.ContentDocumentId;
        }
    }

This renders a broken image in the PDF.

I've also tried to Base64 encode the image and pass that to the template, something like:

<img src="data:image/jpg;base64,{!imageContent}"/>

With the controller code:

    public String imageContent {
        get {
            List<ContentVersion> vers =   [SELECT Id, VersionData FROM ContentVersion ORDER BY CreatedDate DESC];
            ContentVersion v = vers.get(0);
            return EncodingUtil.base64Encode(v.VersionData);
        }
    }

When viewing the visualforce page (I created a tab for it) in both cases the image appears as a broken image using an icon that was created in 1927:

this image is from before when I was born

If I spit out the image base64 contents directly onto the page:

<p>"{!imageContent}"</p>

And I copy that content and then in vanilla HTML I build an image with that base64 encoded data as the src, the image renders properly. So I know this is a valid base64 encoding of the jpg binary data.

Is it possible to embed an image from a File / ContentVersion in a VisualForce rendered PDF?

5

1 Answer 1

1

I was able to preview a entire ContentVersion file using a visualforce page:

Page:

<apex:page controller="PreviewContentVersionCTR">
    <apex:iframe src="data:application/pdf;base64,{!versionData}" height="700px"/>
</apex:page>

Controller:

public class PreviewContentVersionCTR {

    private final String versionData;

    public PreviewContentVersionCTR() {
        ContentVersion cv = [SELECT Id, VersionData FROM ContentVersion LIMIT 1];       
        versionData = EncodingUtil.base64Encode(cv.VersionData);
    }

    public String getVersionData() {
        return versionData;
    }
}

You must log in to answer this question.

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