Newer Version Available

This content describes an older version of this product. View Latest

Converting a Page to a PDF File

You can render any page as a PDF by adding the renderAs attribute to the <apex:page> component, and specifying “pdf” as the rendering service. For example:
1<apex:page renderAs="pdf">
Visualforce pages rendered as PDFs will either display in the browser or download as a PDF file, depending on your browser settings.

In the previous tutorial, you used a Visualforce page to change the name of a company. Suppose you wanted to generate an announcement of the new name as a PDF. The following example produces such a page, along with the current date and time.

1<apex:page standardController="Account" renderAs="pdf" applyBodyTag="false">
2    <head>
3        <style> 
4            body { font-family: 'Arial Unicode MS'; }
5            .companyName { font: bold 30px; color: red; }  
6        </style>
7    </head>
8    <body>
9        <center>
10        <h1>New Account Name!</h1>
11     
12        <apex:panelGrid columns="1" width="100%">
13            <apex:outputText value="{!account.Name}" styleClass="companyName"/>
14            <apex:outputText value="{!NOW()}"></apex:outputText>
15        </apex:panelGrid>
16        </center>
17    </body>
18</apex:page>
Things to note about the page:
  • <style> is CSS markup, not Visualforce markup. It defines the font family used for the entire page, as well as a particular style for the company name.
  • Some of the output text is contained in an <apex:panelGrid> component. A panel grid renders as an HTML table. Each component found in the body of the <apex:panelGrid> component is placed into a corresponding cell in the first row until the number of columns is reached. As there is only a single cell, each output text is displayed in a separate row.
A Visualforce Page Rendered as PDF A Visualforce page rendered as a PDF

Always verify the format of your rendered page before deploying it.