3

I have a table that shows 10 record and whenever i click on 'create PDF' button..whole page is rendered as PDF..But i have a requirement that if i select lets say any 5 record from the table using the check box..then only that records should appear in the pdf

my vf page:

<apex:page controller="queryaccountrecords"  >
<apex:panelGrid columns="2">
<apex:form >
<apex:pageBlock title="Account_Records">
<apex:repeat value="{!accts}" var="dbItem">
<apex:inputCheckbox id="ankita"/>
<apex:outputField value="{!dbItem.Name}" /><br/>
</apex:repeat>
</apex:pageBlock>
<apex:commandButton value="Create PDF" action="{!createpdf}"/>
</apex:form>
</apex:panelGrid>
</apex:page>

my controller:

public class queryaccountrecords {
    public Account[] accts{get;set;}
    public queryaccountrecords()
     {
        accts= [SELECT Name FROM Account LIMIT 10];
    }

    public pagereference createpdf()
    {
    pagereference redirect = new PageReference('/apex/generatepdf');
    redirect.setRedirect(true);
    return redirect;
}
    }

page that is rendered as pdf:

<apex:page controller="queryaccountrecords" renderAs="pdf" showHeader="false" sidebar="false">
<apex:panelGrid columns="2">
<apex:form >

<apex:pageBlock title="Account_Records">

<apex:repeat value="{!accts}" var="dbItem">
<apex:outputField value="{!dbItem.Name}" /><br/>
</apex:repeat>
<apex:commandButton value="Create Pdf" action="{!createpdf}"/>

</apex:pageBlock>
</apex:form>
</apex:panelGrid>
</apex:page>

can someone please help me?

Thanks Ankita Singh

2
  • In the first place you want only selected records to be there in the pdf for that you need to rely on wrapper class. And while rendering them make sure when you click the button render the selected records table for that you can use some flags
    – Suresh
    Jun 3, 2015 at 13:37
  • can i get an idea or approach for the above query? I dont have any idea about wrapper class Jun 3, 2015 at 14:39

3 Answers 3

1

I just went with @KeithC approach and implemented it in my dev org with some modifications. Below is the working code.

First Page:

<apex:page controller="queryaccountrecords">
    <apex:panelGrid columns="2">
        <apex:form >
            <apex:pageBlock title="Account_Records">
                <apex:repeat value="{!accts}" var="dbItem">
                    <apex:inputCheckbox value="{!dbItem.selected}" id="ankita"/>
                    <apex:outputField value="{!dbItem.sob.Name}" /><br/>
                </apex:repeat>
            </apex:pageBlock>
        <apex:commandButton value="Create PDF" action="{!createpdf}"/>
        </apex:form>
    </apex:panelGrid>
</apex:page>

Second page which is rendered as pdf:

<apex:page controller="queryaccountrecords" renderAs="pdf" showHeader="false" sidebar="false">
    <apex:panelGrid columns="2">
        <apex:form >
            <apex:pageBlock title="Account_Records">
                <apex:repeat value="{!accts}" var="dbItem">
                    <apex:outputField value="{!dbItem.sob.Name}" /><br/>
                </apex:repeat>
            <apex:commandButton value="Create Pdf" action="{!createpdf}"/>
            </apex:pageBlock>
        </apex:form>
    </apex:panelGrid>
</apex:page>

Controller:

public class queryaccountrecords {
    public class AccountWrapper {
        public Boolean selected {get; set;}
        public Account sob {get; set;}
        AccountWrapper(Account sob,Boolean selected) {
            this.sob = sob;
            this.selected = selected;
        }
    }
    public AccountWrapper[] accts {get;set;}
    public queryaccountrecords() {      
        accts = new AccountWrapper[] {};
        String s = ApexPages.currentPage().getParameters().get('ids');
        if(s != NULL && s != '') {
            Set<String> ids = new Set<String>(!String.isEmpty(s) ? s.split(',') : new Id[] {});            
            for (Account a : [SELECT Id, Name FROM Account WHERE Id IN : ids]) {
                AccountWrapper w = new AccountWrapper(a,false);
                accts.add(w);
            }
        }
        else {
            for (Account a : [SELECT Id, Name FROM Account LIMIT 10]) {
                AccountWrapper w = new AccountWrapper(a,false);
                accts.add(w);
            }
        }
    }
    public pagereference createpdf() {      
        pagereference redirect = new PageReference('/apex/generatepdf');
        redirect.setRedirect(true);
        Id[] ids = new Id[] {};
        for (AccountWrapper w : accts) {
            if (w.selected)
            ids.add(w.sob.Id);
        }
        redirect.getParameters().put('ids', String.join(ids, ','));
        return redirect;
    }
}

First Page

Second Page rendered as PDF

Hope it helps.

2

As well as needing a wrapper class to record the selection, the selected values need to be passed to the PDF page which will have a separate instance of the controller and so will not have the selections available.

Here is one way to pass the ID values (via a URL parameter):

public class AccountWrapper {
    public Boolean selected {get; set;}
    public Account sob {get; set;}
    AccountWrapper(Account sob) {
        this.sob = sob;
    }
}

public AccountWrapper[] accts {get;set;}

public PageReference createpdf() {
    PageReference pr = Page.generatepdf;
    pr.setRedirect(true);
    Id[] ids = new Id[] {};
    for (AccountWrapper w : accts) {
        if (w.selected) ids.add(w.sob.Id);
    }
    pr.getParameters().put('ids', String.join(ids, ','));
    return redirect;
}

Then in the controller constructor select any IDs passed in the ids parameter so the PDF page can then do its rendering based on the selected flag:

public queryaccountrecords {
    String s = ApexPages.currentPage().getParameters().get('ids');
    Set<String> ids = new Set<String>(!String.isEmpty(s) ? s.split(',') : new Id[] {});
    accts = new AccountWrapper[] {};
    for (Account a : [SELECT Id, Name FROM Account LIMIT 10]) {
        AccountWrapper w = new AccountWrapper(a);
        a.selected = ids.contains(a.Id);
    }
}
1

Here is the link for the Wrapper class implementation

https://developer.salesforce.com/page/Wrapper_Class

This explains very clearly how to implement the wrapper class and for the pdf part you may need to do some more coding .

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .