0

We have a visualforce page to enter notes. On button click, we have to store the page as a word document in salesforce. So we use the getter setter variable contentType to render the page as the word document on button click.

If we change the contentType to application/msword, it directly downloads the word document to the local system. If we open the document and edit it in the word application, it's working fine.

But We need to store the document in salesforce as an attachment, So we use the Apexpages.currentPage().getContent() to get the current page content and store it as an attachment. When we open the generated attachment in the word application it throws some errors like missing file: /sCSS/46.0/sprites/156279453600/Theme3/default/gc/elements.css and displays Html contents.

  1. Generated word document using ContentType. it works fine, but it directly downloaded to the local system. I need to store it in SF. enter image description here
  2. Generated word document using getContent() method in Apex. Using getContent(), we can store it in salesforce, but it shows html contents.enter image description here

    Is there any way to fix this issue.

Page:

<apex:page controller="TemplatePageController" contentType="{!contentType}" applyBodyTag="false" applyHtmlTag="false" showHeader="false" cache="true">
<html xmlns:w="urn:schemas-microsoft-com:office:word">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <head>
        <style type="text/css">
            @page {
                size:A4 portrait;
                @top-center {
                    content: element(header);
                }
                @bottom-left {
                    content: element(footer);
                }
                margin-bottom: 3cm;
            }
            div.header {
                padding: 10px;
                position: running(header);
            }
            div.footer {
                font-size: 13px;
                display: block;
                padding: 5px;
                position: running(footer);
            }
            .pagenumber:before {
                content: counter(page);
            }
            .pagecount:before {
                content: counter(pages);
            }
            img.cusSize {            
                height: 3rem;
                width: 3rem;
            }
            fieldset {
            }
            body {
                font-family: Times New Roman;
                font-size: 13.2px;
            }
            .headerBox {
                padding: 5px;
                border-radius: .25rem;
                background-color: #002060;
                color: white;
            }
            .label {
                color:#002060;
            }     
            table {
                table-layout: fixed ;
                width: 100%;
            }
            .borderTable{ 
                border-collapse: collapse; 
                border: 2px solid #002060;
            }
            .borderTable td { 
                border: 1px solid #002060;
                vertical-align: top;
            }
            .borderTable .element {
                padding: 1rem;
                min-height: 100px;
            }
            .formControll{
                padding-top: 0.5rem;
            }
            .textArea{
                margin: 0px; 
                width: 400px; 
                height: 100px;
            }           
        </style>
    </head>

    <apex:form id="templatePage" >
        <body>
            <div class="content">
                <div class="headerBox">
                    <center>
                        <p>
                            <b>
                                Template Page
                            </b>
                        </p>
                    </center>
                </div>

                <div style="padding: 5px;">
                    <table class="borderTable">
                        <tbody>
                            <tr>
                                <td>
                                    <div class="element">
                                        <span class="label">Notes1</span>
                                        <div class="formControll">                        
                                            <apex:outputPanel rendered="{! not(showDocument)}">
                                                <div>
                                                    <apex:inputTextarea value="{!fieldsWrapper.notes1}" styleClass="textArea"/>
                                                </div>
                                            </apex:outputPanel>
                                            <apex:outputPanel rendered="{!showDocument}">
                                                <div>
                                                    <apex:outputText value="{!fieldsWrapper.notes1}" escape="false"/>
                                                </div>
                                            </apex:outputPanel>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <div class="element">
                                        <span class="label">Notes2</span>
                                        <div class="formControll">                        
                                            <apex:outputPanel rendered="{! not(showDocument)}">
                                                <div>
                                                    <apex:inputTextarea value="{!fieldsWrapper.notes2}" styleClass="textArea"/>
                                                </div>
                                            </apex:outputPanel>
                                            <apex:outputPanel rendered="{!showDocument}">
                                                <div>
                                                    <apex:outputText value="{!fieldsWrapper.notes2}"></apex:outputText>
                                                </div>
                                            </apex:outputPanel>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <div class="element"> 
                                        <span class="label">Notes3</span>
                                        <div class="formControll">                        
                                            <apex:outputPanel rendered="{! not(showDocument)}">
                                                <div>
                                                    <apex:inputTextarea value="{!fieldsWrapper.notes3}" styleClass="textArea"/>
                                                </div>
                                            </apex:outputPanel>
                                            <apex:outputPanel rendered="{!showDocument}">
                                                <div>
                                                    <apex:outputText value="{!fieldsWrapper.notes3}"></apex:outputText>
                                                </div>
                                            </apex:outputPanel>
                                        </div>
                                    </div>
                                </td>
                            </tr>

                        </tbody>
                    </table>
                </div>
           </div>             
            <apex:outputPanel rendered="{!not(showDocument)}">
                <center>
                    <div style="padding-top:10px;">
                        <apex:commandButton action="{! print }" value="Download" />
                    </div>
                </center>
            </apex:outputPanel>
        </body>
    </apex:form>
</html>

Controller:

public with sharing class TemplatePageController {

public TemplateFields fieldsWrapper { get; set; }

public Boolean showDocument { get; set; }

public String contentType { get; set; }

public TemplatePageController(){
    fieldsWrapper = new TemplateFields();
}

public PageReference print() {
    showDocument = true;
    contentType = 'application/msword';
    PageReference currentPage = Apexpages.currentPage();

    Attachment attach = new Attachment();

    Blob body;
    body = currentPage.getContent();

    attach.Body = body;
    // add the user entered name
    attach.Name = 'test.doc';
    attach.ContentType = 'application/msword';
    attach.IsPrivate = false;
    attach.ParentId = '0037F00000Dleg6QAB';
    insert attach;

    return null;
} 

public class TemplateFields{
    public String notes1{ get; set;}
    public String notes2{ get; set;}
    public String notes3{ get; set;}
}

}

0

I can't add comments to ask this question, because of my low reputation, but can you use getContentAsPDF()? does this have to be a word doc?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.