+ Start a Discussion
EchoEchoEchoEcho 

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

Best Answer chosen by Admin (Salesforce Developers) 
prageethprageeth

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

prageethprageeth

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"/>';

Message Edited by prageeth on 09-29-2009 02:32 AM
EchoEchoEchoEcho
Actually, the browser finds it just fine when rendering as HTML, it's the server that doesn't find it while rendering as PDF. We found a solution to the issue by using Documents.
s_foos_foo

Hi,

 

I have a similar kinda issue, can you plz share your solutio to this problem?

Thanx in advance.

 

Thanx

 

S

EchoEchoEchoEcho
The solution is in the previous post. You have to use the Documents tab in order to have the image show up in the PDF. Static Resources won't work, and neither will references to images hosted on external servers.
prageethprageeth

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> 

 

 

This was selected as the best answer
Kirk F.ax361Kirk F.ax361

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!

IPIP

Then how shall i display an image in pdf which is hosted in external server. Pls provide an example.