Rendering a Visualforce page as PDF

In this article we will learn how to render a visualforce page as PDF in Salesforce.

In order to render a visualforce page as PDF you have to set renderAs attribute to PDF.

Have a look at below code example where I have created a Visualforce page which is rendered as PDF. PDF will display the list of 5 accounts fetched using Apex in tabular format.

<apex:page controller="PdfExampleController" renderAs="pdf">
   <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:pageBlockTable>
    </apex:pageBlock>
</apex:page>

Below is the Apex Controller which will fetch 5 account records from Salesforce and are displayed in the rendered PDF.

public class PdfExampleController {
    public List<Account> accList { get; set; }

    public PdfExampleController() {
        accList = [SELECT Id, Name, Type, AccountNumber, AnnualRevenue, Rating FROM Account LIMIT 5];
    }
}

Also, check out the below video on How to Render Visualforce Page as PDF in Salesforce?

Leave a Comment