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:pageBlockTable>
    </apex:pageBlock>
  </apex:form>
</apex:page>

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: https://www.sfdcpoint.com/salesforce/visualforce-page-render-pdf/

4 comments

Skip to comment form

    • Bill on October 31, 2014 at 12:00 pm
    • Reply

    How download the pdf?

    • Lakshmi on June 10, 2021 at 2:10 am
    • Reply

    Can we able to render the input values of the visualforce page to pdf? Please reply
    I am trying to generate pdf from visual force page, but the input values are not printing on the pdf.

    • flavia on July 26, 2021 at 12:46 am
    • Reply

    How can you have the data that you have put in the fields also to show in the VF page ? currently, i am struggling with that.

    • flavia on July 26, 2021 at 12:47 am
    • Reply

    How can you have the data that you have put in the fields also to show in the VF page ? currently, i am struggling with that. kindly assist

Leave a Reply

Your email address will not be published.