function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mohan s 37mohan s 37 

How to display selected account record as a pdf format

Hi, Friends
I want to display selected Account record as pdf format when I select the checkboxes those records only displayed in pdf format. I tried this scenario but the page has not displayed the content of the record. it displays only empty pdf page. Please go through the following code which i had developed in my org. Please let me know what mistake I had made in the code.
Controller
public class PdfController {
 public List<Account> initialList {get;set;}
    public List<Account> selectedlist {get;set;}
    public List<InnerWrapper> wlist {get;set;}
    public PdfController() {
      initialList=new List<Account>();
        wlist=new List<InnerWrapper>();
        InnerWrapper iw;
      initialList=[SELECT Id, Name, Industry FROM Account LIMIT 10]; 
        for(Account a:initialList){
            iw=new InnerWrapper();
            iw.unselected=a;
            wlist.add(iw);
        }
    }
    public PageReference displaySelected() {
        selectedlist=new List<Account>();
        for(InnerWrapper selectedaccount:wlist){
            if(selectedaccount.selectacc){
               selectedlist.add(selectedaccount.unselected);
           }
        } 
        PageReference p=page.pdfpage;
        return p;
    }
   
    public class InnerWrapper {
        public boolean selectacc{get;set;}
        public Account unselected {get;set;}
    }
}
Vfpage1
<apex:page controller="PdfController">
  <apex:form >
    <apex:pageBlock >
        <apex:pageBlockTable title="Accounts" value="{!wlist}" var="iterate">
            <apex:column headerValue="selectAccount">
                <apex:inputCheckbox value="{!iterate.selectacc}"/>
            </apex:column>
            <apex:column headerValue="Name" value="{!iterate.unselected.Name}"/>
            <apex:column headerValue="Print">
                <apex:commandButton value="download" action="{!displaySelected}"/>
            </apex:column>
        </apex:pageBlockTable>
      </apex:pageBlock>
  </apex:form>
</apex:page>
<apex:page controller="PdfController">
  <apex:form >
    <apex:pageBlock >
        <apex:pageBlockTable title="Accounts" value="{!wlist}" var="iterate">
            <apex:column headerValue="selectAccount">
                <apex:inputCheckbox value="{!iterate.selectacc}"/>
            </apex:column>
            <apex:column headerValue="Name" value="{!iterate.unselected.Name}"/>
            <apex:column headerValue="Print">
                <apex:commandButton value="download" action="{!displaySelected}"/>
            </apex:column>
        </apex:pageBlockTable>
      </apex:pageBlock>
  </apex:form>
</apex:page>
Vfpage2
<apex:page controller="PdfController" showHeader="false">
    <apex:pageBlock >   
    <apex:pageBlockTable value="{!selectedlist}" var="a">
   <apex:pageBlockSection columns="2">
       <apex:outputField value="{!a.Name}"/>
        </apex:pageBlockSection>
        </apex:pageBlockTable>
        </apex:pageBlock>
</apex:page>
NagendraNagendra (Salesforce Developers) 
Hi Mohan,

You will need Apex class (Controller Extension) and Visualforce page.
 
1. Create a Custom Button of type List and add on List View. The URL of the button should be your Visualforce page.
2. When you select the contacts and click on "Your Custom Button", the ID of selected Contacts will be available to your Controller Extension.
3. Populate all your record in Visual force and tell render as pdf.
That's it.
 
If you need a tutorial on how to create PDF, please look into below article: Let's take an example of Lead.

Visual Force Page:
<apex:page standardController="Lead" recordSetVar="anyName" extensions="YOUR_CONTYROLLER_EXTENSION_CLASS" >
<!-- Your logic here to display the record using repeater or datatable -->
</apex:page>
Apex Controller Page:
public class YOUR_CONTYROLLER_EXTENSION_CLASS
{
	ApexPages.StandardSetController setCon;  
	
	public YOUR_CONTYROLLER_EXTENSION_CLASS(ApexPages.StandardSetController controller)
    {
		setCon = controller;
		Set<Id> leadIDSet = new Set<id>();
		for ( Lead led : (Lead[])setCon.getSelected() )
        {
               leadIDSet.add(led.id);               
        }
		
		//Here SET leadIDSet contains the ID of all the lead selected
	}

}
Please let us know if this helps.

Regards,
Nagendra.
 
Arvind KumarArvind Kumar
Hi ,

Use <apex:page controller="PdfController" renderAs="pdf"> in vf page. It will for you.
mohan s 37mohan s 37
Thank you Nagendra,  for your quick reply. I really thankful to you spending your valuable time.When I try to populate fields in  visualforce page the values are not displayed in vf page only empty content is displayed.
page
<apex:page standardController="Lead" recordSetVar="leads" extensions="LeadPdfCon" renderAs="pdf">
    <apex:form>
        <apex:pageBlock>
          <apex:pageBlockTable value="{!leads}" var="led">  
                <apex:outputText value="{!led.LastName}"></apex:outputText>
                <apex:outputText value="{!led.status}"></apex:outputText>
           </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>
NagendraNagendra (Salesforce Developers) 
Hi Mohan,

Seems everything fine with your visual force page.may I request you to please post the extension "LeadPdfCon" controller so that we can look into it and do the needful.

Regards,
Nagendra.
mohan s 37mohan s 37
Hi Nagendra,
I am using the same code that you are posted.
public class LeadPdfCon {
  ApexPages.StandardSetController setcon;
    Public list<Lead> listtodisplay{get;set;}
    public LeadPdfCon(ApexPages.StandardSetController controller){
        setcon=controller;
        Set<Id> leadIdSet=new Set<Id>();
        for(Lead l:(Lead[])setcon.getSelected()){
            leadIdSet.add(l.Id);
        }
        
    }
}
I am grateful to you for your quick response.