How to Create Word, PDF or Excel Files with Salesforce Data

One of challenges in any software application is being able to export data into different documents using templates for various purposes.

An example of this is using such templates to create custom documents such as Fax sheets, Quotes, Invoices, Forms that are required to be filled out by a person in traditional ways.

Visualforce pages allow us to generate such documents combined with Salesfoce data. The documents can be formatted in number of types such as PDF, Excel, Word, HTML or XML.

In this example I will show how easy it is to generate PDF documents using Visualforce pages adn with adding little bit of your coding skills you can easily create other formats as well (of course with certain limitations).

In order to be able to create PDF pages in Salesforce you meed to know the following:
  • When using the Page tag, you should set the "showHeader" attribute to "false". By doing this you are telling to Visualforce to do not render any Salesforce header or even HTML tags.
    So think about a normal HTML page, which has tags such as "html", "head" and "body" none of these tags are added to your page automatically anymore. So we should add them to the page ourselves when creating PDF files.
  • Also note that HTML form elements or Apex input controls can not be included into your document.
  • The other important thing is styling, in order to make your PDF file look professional you need to apply styling skills and add CSS classes to your Visualforce page.
  • And the last point is to set your "Page" attribute called "renderAs" to "pdf".
Below is a sample PDF generate code with no content:



<apex:page showHeader="false" renderAs="pdf">
<head>
</head>
<body>
</body>
</apex:page>



The next step is to add some styling/formatting options as well as planning for your content.

By applying CSS into you can define a few CSS classes that formats your PDF document the way you need.

However, the following are important features you may want to benefit from, such as the ability to set the PDF file pages orientation (Landscape or portrait), allow page numbers or additional descriptions in PDF file's header or footer, etc.

Some of these examples are shown below:


<apex:page showHeader="false" renderAs="pdf">
<head>
<style type="text/css">
@page
{

/* Landscape orientation */
size:landscape;

/* Put page numbers in the bottom right corner of each
page in the pdf document. */
@bottom-right {
content: "Page " counter(page);
}
}

body {
font-family: Arial Unicode MS;
font-size:9pt;
}


td {
font-size:11pt;
font-family:Tahoma;
}

/* you can even define custom classes that utilize your static resources */
.checkbox
{
width: 19px;
height: 16px;
background-image:url({!URLFOR($Resource.Checkbox)});
background-repeat: no-repeat;
}
</head>
<body>
</body>
</apex:page>



If your requirement is to create Word or Excel files the process would be the same only following changes are required:
  • Remove "renderAs" attribute
  • Add a new attribute to the page tag: "ContentType":
    - For Word: contentType="application/msword"
    - For Excel: contentType="application/x-excel"
  • And finally a little bit of tweaking of your style will take care of the job