Feb 15

Print this Post

Visualforce page render as pdf example

Visualforce page render as pdf example

We can render any page as pdf  by adding renderAs attribute to <apex:page> and specifying “pdf” as value. renderAs value is name of any supported content converter. Currently PDF is the only supported content converter. Setting renderAs attribute to “pdf” renders the page as a PDF.

Rendering a Visualforce page as a PDF is intended for pages that are designed and optimized for print. Standard components that are not  easily formatted for print or contain form elements like inputs, buttons, any component that requires Javascript to be formatted, should not be used.  We should always verify the format of your rendered page before deploying it.

Sometimes PDF fails to display all the characters properly. In that case we should adjust the fonts in our CSS to use a font that supports our needs. We can use style element for displaying content properly in PDF.

In the example below, we are showing 10 account in table in visualforce page which is renderAs “pdf”. We are just adding renderAs attribute in apex:page and giving “pdf” as its value.

Click for Demo

Visualforce Page

<apex:page controller="PdfExampleController" renderAs="pdf">
  <apex:form >
    <apex:pageBlock >
        <apex:pageBlockTable value="{!accList}" var="acc" border="2">
           <apex:column value="{!acc.name}"/>
           <apex:column value="{!acc.annualrevenue}"/>
           <apex:column value="{!acc.type}"/>
           <apex:column value="{!acc.accountnumber}"/>
           <apex:column value="{!acc.rating}"/>

Apex Code:

public with sharing class PdfExampleController {
    public List accList{get;set;}
    public PdfExampleController (){
        accList = [select id,name,type,accountnumber,annualrevenue,Rating from account limit 10];

Please note that this page is not optimized to be renderAs pdf. Salesforce has provided Best Practices for Rendering PDFs.

Permanent link to this article: http://www.sfdcpoint.com/salesforce/visualforce-page-render-pdf/

  • Bill

    How download the pdf?