1

I am trying to display an image in my PDF but am unsuccessful. I am using files to attach the images. Can someone please find out where I am going wrong?

Apex code:

public class findImageAttach {
    public ID accountID {get; set;}
    public String imageURL{get;set;}

    public findImageAttach(ApexPages.StandardController controller){

        accountID = ApexPages.currentPage().getParameters().get('id');
        List<ContentVersion> documentList = new List<ContentVersion>();

            AggregateResult[] groupedResults = [SELECT COUNT(ID) FROM ContentVersion where Account_Name__r.id =: accountID];
            System.debug('groupedResults size: ' + groupedResults.size());
            if(groupedResults.size() > 0)
            {
                imageURL='/servlet/servlet.FileDownload?file=';
                documentList=[SELECT id, Title, ContentDocumentId, ContentDocument.id,ContentDocument.LatestPublishedVersionId FROM ContentVersion ORDER BY CreatedDate DESC LIMIT 1];
                System.debug('documentList title: ' + documentList[0].Title);
                System.debug('ContentVersion id: ' + documentList[0].id);
                System.debug('ContentDocumentId from ContentVersion: ' + documentList[0].ContentDocumentId);
                System.debug('ContentDocument.id: ' + documentList[0].ContentDocument.id);
                System.debug('ContentDocument.LatestPublishedVersionId: ' + documentList[0].ContentDocument.LatestPublishedVersionId);

                if(documentList.size()>0)
                {
                  //imageURL=imageURL+documentList[0].id;
                  imageURL=imageURL+documentList.get(0).ContentDocumentId;
                  System.debug('imageURL: ' + imageURL);

                }  
            }  
    }    
}

VF code:

<apex:page standardController="Account" showHeader="false" standardStylesheets="false" renderAs="pdf"  id="mypage" applyBodyTag="false" extensions="findImageAttach">        
    <body>

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


    </body>
</apex:page>

I believe the problem is I am using the incorrect id for the attached image and therefore it doesn't render in VF properly.

2

In my experience, the PDF generation code (run when renderAs="pdf" is specified) can only access public image URLs and not images protected by Salesforce's normal login authentication.

To have those images hosted in Salesforce, we use public Document objects as follows:

  • Go to the Document tab (this can be accessed via the '+' overflow tab) and create a new Document. Ensure 'Externally Available Image' checkbox is ticked and upload the required image to be saved with the Document.
  • To get the URL for the image to use go to the Document list view and right click on the 'View' link beside the appropriate Document. Select 'Copy Link Address' to copy the URL to the clipboard.

The URL will be something like:

https://ORG-DOMAIN/servlet/servlet.FileDownload?file=015R00000004CJW

so given the Id of the Document you can create the URL in your logic.

Note that these documents are visible to anyone who can work out the URL, so nothing confidential or personally identifying should be in the images. If your images contain such information, you will need to look for a different approach.

0

For me it worked with a png file like this:

imageURL='/sfc/servlet.shepherd/version/renditionDownload?rendition=ORIGINAL_Png&versionId='+documentList.get(0).LatestPublishedVersionId;

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.