Visualforce Page Rendered as PDF

We can render any page as pdf by adding renderAs attribute to <apex:page> and specifying “pdf” as value. renderAs value is the 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.

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

Visualforce Page:

<apex:page controller="AccountPDFController" renderAs="pdf">
  <apex:form >
    <apex:pageBlock >
        <apex:pageBlockTable value="{!accountList}" 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:column value="{!acc.Phone}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
  </apex:form>
</apex:page>

Apex Code:

public with sharing class AccountPDFController{
    public List accountList{get;set;}
    public PdfExampleController (){
        accList = [select id,name,type,accountnumber,annualrevenue,Rating,Phone from account limit 5];
    }
}
Leave a Reply

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