Find your content:

Search form

You are here

Opening a word document dynamically generated from visualforce in Ms Word in print view/print layout by default

 
Share

Hope this scenario will be very interesting for everyone!! I have a scenario where the user needs to download the word document by clicking a link on page.

This word document is dynamically generated from visualforce by fetching values from the record.

Everything works fine. But the problem is when I try to open the downloaded document in MsWord (2007 or 2010), It opens in web layout by default. But I need to open the document in print layout / print view.

I searched and found that the below code snippet works well for all kinds of technology like php, python, etc..

// Code snippet

<!--[if gte mso 9]>
<xml>
<w:WordDocument>
  <w:view w:val="print" />
  <w:zoom w:percent="100" />
  <w:DoNotOptimizeForBrowser/>
  <w:DoNotHyphenateCaps/>
  <w:PunctuationKerning/>
</w:WordDocument>
</xml>
  <![endif]-->

But it didnt work with visualforce. I placed this code snippet inside the tag and right below the tag as per Office XML reference. But I didnt get a positive result.

Is there anyway to achieve this in visualforce??


Attribution to: Logeswaran Udayakumar

Possible Suggestion/Solution #1

Please Refer The following. You Need to add HTMl attributes

http://boards.developerforce.com/t5/Visualforce-Development/Rendering-VF-as-Word-Document/td-p/450381


Attribution to: M.sf

Possible Suggestion/Solution #2

If you want to download page as word file use

contenttype = application/msword#FileName.doc

in visualforce page tag

and see this Answer to generate downloadable page.

Thanks


Attribution to: Axaykumar Varu

Possible Suggestion/Solution #3

The snippet you've got is correct, but as you say it won't save in a Visualforce page.

Instead you can great a read only property in your vf controller like so:

//controller
public String getWordPrintViewXML()
{
    // doesn't need to be on multiple lines, it's just for readability
    return '<!--[if gte mso 9]>' +
        '<xml>' +
        '<w:WordDocument>' +
        '<w:View>Print</w:View>' +
        '<w:Zoom>100</w:Zoom>' +
        '<w:DoNotOptimizeForBrowser/>' +
        '</w:WordDocument>' +
        '</xml>' +
        '<![endif]>';
}

... and then use <apex:outputText> with escape="false" to insert it into the page. Normally you should avoid the use of escape="false" as it can be used in nasty ways for injection attacks, but this is a pretty valid use case since we're using a fixed string:

<!-- Page code -->
<apex:outputText value="{!WordPrintViewXML}" escape="false"/>

Attribution to: Matt Lacey
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4827

My Block Status

My Block Content