Visualforce

Rendering a Visualforce page as PDF

How to render 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?

Shubham Lashkan

Shubham Lashkan is a Salesforce Developer at Advanz101 with four years of experience. He's worked on various Sales Cloud projects and integrations with platforms like Facebook, Jira, and Twitter. He regularly shares his knowledge through articles and videos on his blog, YouTube channel, and Apex Hours.

Leave a comment

Your email address will not be published. Required fields are marked *