I have come across a scenario image is not appearing in pdf sometimes.For this I have shared some tricks.
It is very easy to display an image in Visulaforce page.
Process
<table>
<tr>
<td>
<div>
<apex:image value="{!sLogo}" width="180" height="60" />
<!--<apex:image url="https://ap1.salesforce.com/resource/logo" width="180" height="60" />-->
</div>
</td>
</tr>
</table>
</apex:page>
public String sLogo {get;set;}
public ImageController(){
list<StaticResource> listOfStaticResources = [SELECT Id,SystemModStamp
FROM StaticResource
WHERE Name = 'logo'];
if(!listOfStaticResources.isEmpty()) {
sLogo = '/resource/'+listOfStaticResources[0].SystemModStamp.getTime()+'/logo';
System.debug('Printing after slogo'+sLogo );
} else {
//sLogo = (invoiceit_s__Configuration__c.getValues('COMPANY_LOGO').invoiceit_s__String_Value__c);
}
}
}
It is very easy to display an image in Visulaforce page.
Process
- Crate a static resource and store that image
- Click in View file link
- Copy the url
- Add apex:image tag in vf page.
Image will be displayed. When you render that page as pdf then sometimes image will not be appear.
To appear image every time, I have done some work around and I would love to share it.
Lets talk about the image url --- "https://ap1.salesforce.com/resource/1380178695000/logo".
All must be familiar with each term except this 1380178695000. What is that value?
This is nothing but the lastmodifiedtime of static resource.1380178695000 must be dynamic. To get dynamic value I have used some function SystemModStamp.
Here is my page and Controller.
<apex:page controller="ImageController" renderAs="pdf" ><table>
<tr>
<td>
<div>
<apex:image value="{!sLogo}" width="180" height="60" />
<!--<apex:image url="https://ap1.salesforce.com/resource/logo" width="180" height="60" />-->
</div>
</td>
</tr>
</table>
</apex:page>
Controller is
public with sharing class ImageController {public String sLogo {get;set;}
public ImageController(){
list<StaticResource> listOfStaticResources = [SELECT Id,SystemModStamp
FROM StaticResource
WHERE Name = 'logo'];
if(!listOfStaticResources.isEmpty()) {
sLogo = '/resource/'+listOfStaticResources[0].SystemModStamp.getTime()+'/logo';
System.debug('Printing after slogo'+sLogo );
} else {
//sLogo = (invoiceit_s__Configuration__c.getValues('COMPANY_LOGO').invoiceit_s__String_Value__c);
}
}
}
Now image will appear in pdf all time.
No comments:
Post a Comment