You need to sign in to do that
Don't have an account?
Static Resources in APEX PDF generation with Sites
I have a Sites page that dynamically generates a PDF from a custom CMS. As part of that page, I would like to dynamically include certain images throughout, accessed from Static Resources, in sections of HTML generated by the Apex Controller through the use of <apex:outputText value="{!someGetter}" escape="false" />.
If the page is not set to renderAs="pdf", I can access Static Resource images in the return value of the Apex method like this:
return '<img src="resource/ResourceName">';
However, if I set renderAs="pdf" so that the page renders as PDF, the images show up broken. I've tried a few different ways of accessing the Static Resource, including the full URL, as well as linking to images off-site, like the Google Logo. I've even tried embedding the image into the text, using Base64. It seems like the only way to include an image in a PDF is to have it directly in the VisualForce page, which is inconsistent with the rendering of a regular VF page. Is this a bug, or is there some way to accomplish what I'm trying to do here?
Thanks,
Tom
Hello s_foo;
If you like to keep your image in a zip file you can use the following solution. You have to keep your image in a zip file and keep the zip file in static resources. Then
Lets assume the name of your zip file is myZipFile. Then,
Controller:
global class MyController{
public String getImageName() {
return 'Picture.gif';//this is the name of your image
}
}
Page:
<apex:page renderAs="pdf" controller="MyController">
<apex:variable var="imageVar" value="{!imageName}"/>
<apex:image url="{!URLFOR($Resource.myZipFile, imageVar)}"/>
</apex:page>
All Answers
Your code
return '<img src="resource/ResourceName">';
doesn't work because the browser can't find the path.
Your path should be something like this.
return '<img id="myImage" src="/resource/1254211771000/myLogoName"/>';
Hi,
I have a similar kinda issue, can you plz share your solutio to this problem?
Thanx in advance.
Thanx
S
Hello s_foo;
If you like to keep your image in a zip file you can use the following solution. You have to keep your image in a zip file and keep the zip file in static resources. Then
Lets assume the name of your zip file is myZipFile. Then,
Controller:
global class MyController{
public String getImageName() {
return 'Picture.gif';//this is the name of your image
}
}
Page:
<apex:page renderAs="pdf" controller="MyController">
<apex:variable var="imageVar" value="{!imageName}"/>
<apex:image url="{!URLFOR($Resource.myZipFile, imageVar)}"/>
</apex:page>
Nice one, Prageeth! I confirm this works well. We have a static resource of hundreds of small image files, and this technique is letting us have the right one appear in the visualforce page -- rendered as HTML or PDF. This made my day!
Then how shall i display an image in pdf which is hosted in external server. Pls provide an example.