0

I have written code which will generate PDF for selected Apexclass.Problem is it is generating pdf with all Apexclasses not selected one.

I am sharing code.

vf page

<apex:page controller="generatePdfCtrl" sidebar="false" >
  <apex:form >
    <apex:pageBlock >
      <apex:pageBlockButtons >
        <apex:commandButton value="Genrate PDF" action="{!GeneratePDF}" rerender="a"/>
      </apex:pageBlockButtons>
      <apex:pageBlocksection>
        <apex:pageBlockTable value="{!wp}" var="w" id="a">
          <apex:column >
            <apex:inputCheckbox value="{!w.selected}"/>
          </apex:column>
          <apex:column value="{!w.ac.Name}"/>
        </apex:pageBlockTable>
      </apex:pageBlocksection>
    </apex:pageBlock>
  </apex:form>
</apex:page>

controller

public class generatePdfCtrl {

    public List < WrapPDFClass > wp {
        get;
        set;
    }

    public List < apexclass > apexcls {
        get;
        set;
    }

    public generatePdfCtrl() {
        apexcls = new List < apexclass > ();
        wp = new List < WrapPDFClass > ();
        apexcls = [select Name, Status, CreatedDate, CreatedBy.Name from apexclass where Status = 'Active'];

        if (apexcls.size() > 0) {
            for (apexclass a: apexcls) {
                WrapPDFClass we = new WrapPDFClass(a);
                wp.add(we);
            }
        }

    }


    public PageReference GeneratePDF() {
        List < apexclass > apx = new List < apexclass > ();
        List < WrapPDFClass > tempwrap = new List < WrapPDFClass > ();

        for (WrapPDFClass wl: wp) {
            if (wl.selected == TRUE) {
                apx.add(wl.ac); {
                    for (apexclass al: apx) {
                        PageReference pg = Page.Generate_PDF;
                        Blob pdf1 = pg.getcontentAsPDF();

                        Document d = new Document();
                        d.FolderId = UserInfo.getUserid();
                        d.body = pdf1;
                        d.name = 'Demo PDF';
                        d.contenttype = 'application/pdf';
                        d.type = 'pdf';
                        insert d;
                    }
                }
            }
        }

        return null;
    }
    public class WrapPDFClass {
        public apexclass ac {
            get;
            set;
        }
        public boolean selected {
            get;
            set;
        }

        public WrapPDFClass(apexclass a) {
            ac = a;
            selected = false;
        }
    }
}
1
  • 1
    better variable names would aid in debugging list -- wp should be a plural or something like wpcList; wl should be wpc (WrapperPdfClass); I don't follow how what ApexClass has anything to do with the PDF that is rendered as variable al is never used
    – cropredy
    Commented Jul 14, 2015 at 16:49

2 Answers 2

1

Off-hand, it appears to me that there must be an issue with the implementation of this method:

public class WrapPDFClass {
    public apexclass ac {
        get;
        set;
    }
    public boolean selected {
        get;
        set;
    }

    public WrapPDFClass(apexclass a) {
        ac = a;
        selected = false;
    }
}

In your other code, you do this:

public PageReference GeneratePDF() {
    List < apexclass > apx = new List < apexclass > ();
    List < WrapPDFClass > tempwrap = new List < WrapPDFClass > ();

    for (WrapPDFClass wl: wp) {
        if (wl.selected == TRUE) {
            apx.add(wl.ac); {
                for (apexclass al: apx) {

If everything is getting printed whether selected or not, it would seem that your logic in one or the other of these code sections isn't correct. Perhaps public WrapPDFClass(apexclass a) needs to be revised to have selected = true;?

I'd have expected all of your selection logic to create your list to have happened in that method rather than essentially repeating it again in your PageReference GeneratePDF() method by where you have these two lines of code:

         if (wl.selected == TRUE) {
            apx.add(wl.ac); 
0

For every selected class, your GeneratePDF method adds the new class to the apx list and then generates a PDF for every class in the apx list. So as the list grows you are repeating the PDF generation over and over again for the early elements of the list.

Also there is no parameter passing from your loop into the Generate_PDF page so I assume it is generating the same fixed content on every call. You need to pass to it a parameter that allows it to query the right class information.

So I think your method should look like this:

public PageReference GeneratePDF() {
    Document[] docs = new Document[] {};
    for (WrapPDFClass wl: wp) {
        if (wl.selected == TRUE) {
            PageReference pg = Page.Generate_PDF;
            pg.getParameters().put('name', wl.apexclass.name);
            docs.add(new Document(
                    FolderId = UserInfo.getUserid(),
                    body = pg.getcontentAsPDF(),
                    name = wl.apexclass.name + ' PDF',
                    contenttype = 'application/pdf',
                    type = 'pdf'
                    ));
        }
    }
    insert docs;
    return null;
}

ignoring possible heap governor limit problems.

2
  • It is working fine but i want content in my PDF should be Apex Class name..It is showing all classes in PDF whether i have selected class or not. Commented Jul 15, 2015 at 6:52
  • @ShantanuMahajan When you look at the Apex controller for the page Generate_PDF, how does it limit its output to a single class?
    – Keith C
    Commented Jul 15, 2015 at 7:36

You must log in to answer this question.

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