0

enter image description here

I want to show image on visualforce pdf screen. The image file is in the related lists files.

ResumeController.cls

    public with sharing class ResumeController {

    public List<ContentDocumentLink> listCDL{get; set;}

    public ResumeController(ApexPages.StandardController controller){
        listCDL = new List<ContentDocumentLink>();
    }

    public void getResume(){
        String currentId = ApexPages.currentPage().getParameters().get('id');

        //query image id of related lists
        List<ContentDocumentLink> ListOfCDL = [SELECT ContentDocumentId
                    FROM ContentDocumentLink
                    WHERE LinkedEntityId
                    IN (SELECT Id FROM Person__c WHERE Id =: currentId)];

ResumePrint.vfg

<apex:page standardController="Person__c"
           extensions="ResumeController"
           action="{!getResume}"
           applyBodyTag="false"
           applyHtmlTag="false"
           showHeader="false" sidebar="false" standardStylesheets="false"
           contentType="text/html;charset=UTF-8"
           renderAs="pdf">
    <!--    sol.1-->
    <apex:image> url="/servlet/servlet.FileDownload?file="+{!listCDL}</apex:image>
    <!--    sol.2-->
    <apex:image id="theImage" value="{!'/servlet/servlet.FileDownload?file=&'} +{!listCDL}" width="100" height="100"/>

Then it's showing like this. enter image description here

2
  • In my experience the Visualforce PDF generation can only access public image URLs, is your image URL public?
    – Saroj Bera
    Commented Aug 3, 2022 at 2:52
  • Are you talking about changing from Static Resources to public? I didn't have image url and added it to the file right away.
    – where
    Commented Aug 3, 2022 at 4:00

2 Answers 2

1

This one is wrong because the attribute isn't part of the image tag.

<apex:image> url="/servlet/servlet.FileDownload?file="+{!listCDL}</apex:image>

This one is wrong because {! starts and expression and } ends one, so the + (text concatenation operator) doesn't work.

value="{!'/servlet/servlet.FileDownload?file=&'} +{!listCDL}" 

You're probably looking for:

value="{!'/servlet/servlet.FileDownload?file='+listCDL}"

It's been a while since I've used that URL, though, I'd suggest that you enable the "Public Document" checkbox if you're still using Documents, or preferably find the appropriate URLFOR formula, such as mentioned in this Q&A.

2
  • I knew the notation was wrong, thank you, but I still can't see the picture. As you recommended, I brought a public link url similar to Public Document, but it is not shown in the pdf...
    – where
    Commented Aug 3, 2022 at 8:03
  • @where make sure you test the URL in your browser. I know it's tricky to get right in the first place, but if you can see it in the browser, it'll usually work in the PDF, too.
    – sfdcfox
    Commented Aug 3, 2022 at 8:46
0

I succeeded in bringing the picture. Everyone, keep that in mind

Controller.cls

public with sharing class ResumeController {
    public String imageUrl{get; set;}
    ....
    public void getData(){
        String currentId = ApexPages.currentPage().getParameters().get('id');

        //query image of related lists
        List<ContentDocumentLink> ListOfCDL =
                    [SELECT ContentDocumentId
                    FROM ContentDocumentLink
                    WHERE LinkedEntityId
                    IN (SELECT Id FROM Person__c WHERE Id =: currentId) LIMIT 1];
        
        ContentVersion[] cvs
                = [SELECT Id FROM  ContentVersion
                    WHERE ContentDocumentId=:ListOfCDL[0].ContentDocumentId AND IsLatest=TRUE];
        System.debug('cvs'+cvs);

        imageUrl = '/sfc/servlet.shepherd/version/renditionDownload?rendition=THUMB720BY480&versionId=' + cvs[0].Id; }

Print.vfg

<apex:image url="{!imageUrl}" width="150px" height="200px"/>

You must log in to answer this question.

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