+ Start a Discussion
irlrobinsirlrobins 

Unable to display static resource (image, css) when rendering as PDF in Site

Hey all,

 

I have a VF page that includes an image and a stylesheet that when rendered as html in both org (https://emea.salesforce.com/apex/My_Page) and on my site (http://www.mysite.com/My_Page). But when I change the RenderAs to PDF, the images and CSS fail to work (i.e. broken image in pdf and no styling). I have the two resources as static resources. I've also tried putting the image and css files in Documents and make externally accessible (and tested this) but again still they fail in the pdf.

 

Sample code:

<apex:page standardController="Custom_Object__c" showheader="false" renderAs="pdf">
<apex:image url="{!URLFOR($Resource.MyImage)}" />
<apex:image url="http://www.mysite.com/servlet/servlet.ImageServer?id=015200000010duH&oid=00D200000000MZB&lastMod=1302282092000"/>
<img src="http://www.mysite.com/servlet/servlet.ImageServer?id=015200000010duH&oid=00D200000000MZB&lastMod=1302282092000" />
<img src="{!URLFOR($Resource.MyImage)}" />
etc...

 None of these four work when rendered as pdf when accessed through the site.

 

Anyone any ideas as to why this is? How can I get a static resource accessible in pdf when viewed though a site. As I said, viewing the same VF page thru org works fine. It's only when accessed thru site that the issue arises.

 

thanks

R

Best Answer chosen by Admin (Salesforce Developers) 
TheSwamiTheSwami

 

Can you try this:
1. Add the public URL to the front of all URLFOR tags. 
2. Add a remote site for http://www.mysite.com

 

All Answers

irlrobinsirlrobins

I should also add that this works fine in sandbox....

Cory CowgillCory Cowgill

Hmmmm... I got this to work with a static resource and renderAs="PDF" on my VF page.

 

Here is the image I'm using:

 

<img src="{!URLFOR($Resource.WebResources,'webResources/Images/logoImage.jpg.jpg')}"/>

 

Is your static resource set to "Public" and not "Private"?

irlrobinsirlrobins

Yes, resource is public. I notice your image is in zip. can you try with an image that's not in a zip pls?

Cory CowgillCory Cowgill

Try wrapping it with HTML tags like so...... I can't remember why I did it like this, but I remember fuddling around with the VF/HTML to get the styles to apply in PDF rendered when regular VF rendering worked fiine.......

 

<apex:page standardController="Object__c" renderas="PDF" showHeader="false" sidebar="false">
    <html>
        <head>
            <style>
                body { font-family: Arial Unicode MS;
                       font-size: 10px;
                     }
                td {width:50%; vertical-align:top;}
            </style>
        </head>
        <div id="reportHeader">
            <div id="titleDiv" style="float:left;">
                <img src="{!URLFOR($Resource.WebResources,'webResources/Images/logoImage.jpg.jpg')}"/>



.....
</html>
</apex:page>

irlrobinsirlrobins

I've gone back to my sandbox and retested.

 

<img src="{!$Resource.Myimage}" />
<apex:image url="{!URLFOR($Resource.Myimage)}" /> 

 Both of these work fine in both the sandbox org and thru sandbox site, in html and pdf

 

But on production, they only work in html.

Cory CowgillCory Cowgill

1. Does the Type show up as image/png or image/jpg or whatever in the Static Resource Screen? If Salesforce doesn't detect the type and apply it properly on the static resource I've seen issues. Maybe you can zip it up and try using it in a zip? (I can't unzip and test ming at the moment, sorry)

 

2. Did you try that wonky HTML wrapping I posted above?

 

3. Have you tried making sure the API version is saved to Version 21 for the VF Page?

 

Just spitballing those ideas now..... good luck!

irlrobinsirlrobins

1. Yes/ Also tried zip and again in prod it works fine in html but not in pdf

2. Yes, no luck

3. It was saved as 20.0, changed to 21.0 and same result

 

 

TheSwamiTheSwami

 

Can you try this:
1. Add the public URL to the front of all URLFOR tags. 
2. Add a remote site for http://www.mysite.com

 

This was selected as the best answer
irlrobinsirlrobins

Hurrah! That worked! :)

 

Thank you

 

So can you explain why I need to do this, in particular why does it render ok as html, but not for pdf? And why I didn't have to add the site url in the sandbox and yet it worked fine?

TheSwamiTheSwami

Glad to hear.  This is a workaround for a potential problem we are investigating.

 

When a PDF is rendered by VisualForce, the server makes outbound requests to include the images and stylesheets.  When creating PDFs within Sites, the URL for the outbound request seems to be generated incorrectly.  Using the workaround I described forces it to use a valid URL.

 

I'm not sure why sandbox worked, but it may have a different URL configuration for Sites, which avoids the problem seen on production.

irlrobinsirlrobins

Thank you