+ Start a Discussion

Rendering Visualforce into word document to show images

Hi Everyone,


I am trying to create a contact directory to show list of contacts along with their pics(from attachment). I want it to be get printed in Word document. I am facing two problems:


1. Images are not shown whereas it seems fine on visualforce page(without renderingas).

2.Page is getting downloaded as xyz.wiz whereas it should be xyz.doc


For 1st problem i have tried the following code:


Here wraplist is wrapper list having contact details along with attachment image


<apex:dataTable value="{!WrapList}" var="contact" width="100%" align="center" style="page-break-inside:avoid;">

<apex:column rowspan="2" style="page-break-inside:avoid;">

<table >

<tr style="page-break-inside:avoid;">
<apex:outputPanel rendered="{!if(contact.photo != null, true,false)}">


// here photo is a string equals '/servlet/servlet.FileDownload?file='+picMap.get(c.id).id;

<img id="theImage" src="{!contact.photo}" width="120" height="130" />  



<style= "align: center;"/>
<b><apex:outputText value="{!contact.con.Name}"/></b><br></br>





Final output should be like following:


Image 1 : Contact1                                                                               Image 2: Contact2

Image 3 : Contact3                                                                               Image 4: Contact4


For second problem i have no idea how to resolve it: 

Only sometime it seems fine with crome rest of time format is not .doc


There is one other minor issue that when no of record are odd alignment for last records get disturbed. 


Any help will be appreciated. Thanks in Advance



Bhisham Bajaj


You cannot simply render into a Word Document, either the old format (proprietary) or the new format. This problem stems from the fact that both formats require binary manipulation in order to be successful.

Visualforce will honor a content-type request of application/msword or or the equivalent application/vnd.msword* mime types, but the server won't know how to format the file correctly, so its output will be raw HTML, which won't load directly in Microsoft Word.

A better option is to render into a pdf (using renderAs="pdf") instead; this provides your original HTML markup rendered as a PDF file, including the images. Or, you can try to convince the web browser to download the file using contentType="application/octet-stream#myfile.html", which will cause the file to download (which may then open in Word if the system is configured correctly).