Sunday, January 8, 2012

Visualforce to Word


I've been having lots of fights with Microsoft applications lately.  Perhaps as a web developer, this goes without saying.  I just won a fight getting a Visualforce page to display correctly when converted to a Word file.  I was generating a file of certificates with each certificate on a separate page and a very specific layout that had to be followed. Thought I'd share a few tips on what I learned.

Basics
First off, how to get a Visualforce page to generate as a Word file.  In the "page" tag, include the following:

<apex:page contentType="application/msword#FileName.doc" showHeader="false" standardStylesheets="false">

Page Formatting
  • Save a Word doc as HTML
    Not sure how to get the font, border, layout, etc specified in Visualforce to display correctly in Word?  Do it in Word and then save it as an HTML file.  The file it generates is a bit of a beast, but if you scroll down, you'll find <style> tags with Word-specific attributes you can use in <style> tags in Visualforce. You can also copy and paste paragraphs from the html into your Visualforce, though I wouldn't recommend trying to paste the whole file, there's just too much crap (very technical term) in there and Salesforce won't even let you save it the way Word writes the html tags.

    For my document, I grabbed these styles to create the following in Visualforce; note the orientation style to set the page as landscape:
    <style>
            @page Section1{
                size:11.0in 8.5in;
                mso-page-orientation:landscape;
                margin:.25in .25in .25in .25in ;
                mso-header-margin:.5in;
                mso-footer-margin:.6in;
            }
            div.Section1
            {
                 page:Section1;
            }
            body {
                font-family:"Gotham-Book";
            }
    </style>
    For the style above to work, you'll need to surround your content in Visualforce with: 
<div class="Section1">your content</div>
  • Use "in" (inches) for widths to layout the page
    Word is designed to render documents for printing, so widths should be specified in inches (and I'm guessing centimeters for everyone that's abandoned our archaic measuring system).  Just a note that "px" does work in Word (in specific contexts) but if you're trying to fit something to a page like a border that goes around everything, save yourself the trouble and just do it in inches.
  • Use "pt" for font sizes
    Setting the font size using style="font-size:12pt;" in VF is the equivalent of selecting 12 for the font size in Word.
  • Margin instead of Padding Padding wasn't doing the trick to space out lines of text but margin works.  Here's a sample:

  • <p style="font-size:22.0pt;font-family:Gotham-Medium;margin-top:12pt;margin-bottom:.0001pt;">
                    Some text here
    </p>
  • Page breaks
    Wherever you want a page break, insert this:

    <br clear="all" style="mso-special-character:line-break;page-break-before:always;"/>

    Note that I tried using "page-break-before:always;" as the style for a div and that was a no go, Word wants it in a break or paragraph tag.
Including Images
  • Don't use a secure URL (https) as the image URL
    I came across forum posts where people were able to successfully use the secure Salesforce URL (i.e. https://na2.salesforce.com/...) to link to images in Word, so my guess is that this varies based on the version of Word you have.  I have Word 2007 and the images would not display until I switched to a non-secure URL, which I did using Salesforce Sites.  It's probably a good idea to do this even if the secure URL works for you because it may not work for others.  
  • Use the full URL
    If your image is stored in Salesforce Documents, you can generate that URL like this:
    http://yourdomain.force.com/servlet/servlet.ImageServer?id=015D0000000Dpwc&oid=00DD0000000FJbGyourdomain = 
    Your Salesforce Sites domain
    id= The Id of the Document
    oid= Your Organization Id, which you can find in Setup in Company Information

    You can also store the image as a Static Resource and link to it that way.
Exclude "Visualforce" from your web searches
Still need help formatting your Visualforce page for Word? When it comes to figuring out formatting in Word, Google (as is often the case) is your friend and no doubt how you arrived here, but don't limit your searches by including "Visualforce" in the search terms. People use html to generate Word files in other languages, and some of the best tips I found were on PHP and ASP forums.

6 comments:

  1. Hi, If i use page size as landscape i am getting header and footer end of the document. if i remove page size landscape it will not get. how can i remove header and footer end of the document.

    ReplyDelete
  2. I don't know offhand, Microsoft Word is a mysterious beast. I'd recommend getting the layout the way you want it in Word, save as HTML and then compare the way they do the header to the way you have it.

    ReplyDelete
  3. Hi, i was wondering if you found a way to make the image positioning work. Cause i tried many css props to make on of my image stand at the right top corner of my doc and it just won't work.

    ReplyDelete
  4. Finally found a trick using table. As the image only will align with the text i created a tab with 2 column only. In the first on i put my text to the left and in the second on i put my picture and set up the the align-text to the left.
    I guess i'll be using a lot of tables since it didn't work with any other tag.

    ReplyDelete
  5. Hi again,

    I have another question did you find a way to put an image on the background of the page?

    regards,

    ReplyDelete
  6. how to add background image to word document from visual force page

    ReplyDelete