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
Niki Shah 12Niki Shah 12 

how to add image in apex code

<apex:page Controller="OrderAcknowledgement" renderAs="pdf" applyBodyTag="false" docType="html">

    <head>

        <style>

            body { font-family: 'Arial Unicode MS'; }

            .companyName { font: bold 30px; color: red; } 

        </style>

    </head>

    <body>

        <center>

        <h1>New Account Name!</h1>

    <apex:image id="theImage" value="{!IMAGEPROXYURL('https://swifterfans.com/wp-content/uploads/sites/3/2018/10/SWIFTER-FINAL-SD.png')}" alt="Description of image here"/>

 <img src="https://swifterfans.com/wp-content/uploads/sites/3/2018/10/SWIFTER-FINAL-SD.png" alt="Description of image here"/>

        <apex:panelGrid columns="1" width="100%">
   
           <apex:outputText value="{!a.Name}" styleClass="companyName"/>

            <apex:outputText value="{!NOW()}"></apex:outputText>

        </apex:panelGrid>

        </center>

    </body>

</apex:page>User-added image
Ramesh DRamesh D
Hi @Niki,
When rendering vf pages as pdf you must use static resource, something like below 
<img src="{!URLFOR($Resource.PdfImage,'imagePdfFile.png')}" style="height:50px;"/>

I hope you find the above solution helpful. If it does mark as best answer to help others too.
Thanks,
Ramesh D
Ramesh DRamesh D
User-added image
Niki Shah 12Niki Shah 12

Error: Static Resource named PdfImage does not exist. Check spelling
Ramesh DRamesh D
@Niki
you must zip all your images and upload under setup--> static resources-->New

Name the zip folder as PdfImage or what ever you wish then replace 'imagePdfFile.png' this to the image name 
Follow this link
https://ccoenraets.github.io/salesforce-developer-advanced/Using-Static-Resources.html

Thanks
Ramesh
Ajay K DubediAjay K Dubedi
Hi Niki,
Try this code it is showing the image.
<apex:page renderAs="pdf" showHeader="false" standardStylesheets="false">
    <html>
        <body >
            <!-- <apex:image value="{!URLFOR($Resource.PdfImage)}" width="300" height="40"/>-->
            <header><img src="{!URLFOR($Resource.PdfImage,'imagePdfFile.png')}" style="height:50px;"/></header>
          </body>
    </html>
</apex:page>
User-added image

For static resource you -
 - First you should create a zip file for you image.
 - Search static resource in Quick find box.
 - Click new and upload zip file .
 
If you face any problem then complete this trialhead.
https://trailhead.salesforce.com/en/content/learn/modules/visualforce_fundamentals/visualforce_static_resources
 
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Deepali KulshresthaDeepali Kulshrestha
Hi Niki,

Greetings to you!

I Have tested with two lines; it is working for me in chrome. Which browser are you using?

Please copy paste this code and see if it works.

    <apex:page >
    <apex:image value="{!URLFOR($Resource.vfimagetest,'cats/kitten1.jpg')}"/>
    <apex:image value="{!URLFOR($Resource.vfimagetest,'cats/kitten1.jpg')}"/>
    </apex:page>

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.