1

Visualforce

<apex:page controller="GeneratePdf">
<script>
    function checkAll(ele)
    {
        var c = document.getElementsByTagName('input');
        // var d = document.getElementsByName('thepagecheck');
        //console.log(d[0]);
        if(ele.checked)
        {
            //alert(c.length);
            for (var i = 0; i < c.length; i++) 
            {
                 if (c[i].type == 'checkbox') {
                     c[i].checked = true;
                 }
            }
        } 
        else 
        {
             for (var i = 0; i < c.length; i++) {
                 console.log(i)
                 if (c[i].type == 'checkbox') {
                     c[i].checked = false;
                 }
             }
         }
   }
</script>
<apex:form id="theForm">
<apex:pageBlock id="pageblockid">
    <apex:pageBlockButtons >
        <apex:commandButton value="Generate PDF" action="{!generate}"/> 
    </apex:pageBlockButtons>

  <apex:pageBlockTable value="{!wrapList}" var="a" style="border-width:thin;border-style:solid;width:100%;border-color:gray;" id="pageblocksectionid">
     <apex:column >
         <apex:inputCheckbox value="{!a.ischeck}" id="thepagecheck"/>
         <apex:facet name="header">
             <apex:inputCheckbox onchange="checkAll(this)" />
         </apex:facet>

     </apex:column>
    <apex:column value="{!a.ac.name}" headerValue="Apex Classes"  />
    <apex:column value="{!a.ac.createdby.name}" headerValue="Created By" />
    <apex:column value="{!a.ac.createdDate}" headerValue="Created Date" />

    </apex:pageblockTable>


    <apex:pageBlockTable value="{!pageList}" var="b" style="border-width:thin;border-style:solid;width:100%;border-color:gray;">
        <apex:column >
             <apex:facet name="header">
                 <apex:inputCheckbox />
            </apex:facet>
            <apex:inputCheckbox value="{!b.ischeck}"/>
        </apex:column>
        <apex:column value="{!b.ap.name}" headerValue="Apex Pages"/>
        <apex:column value="{!b.ap.createdby.name}" headerValue="Created By" />
        <apex:column value="{!b.ap.createdDate}" headerValue="Created Date"/>
    </apex:pageblockTable> 
  </apex:pageBlock>
 </apex:form>

Controller

public class GeneratePdf{
public List <wrapClassPages> pageList {get;set;}
public List <wrapClassPages> wrapList {get;set;}

public List <String> a;
public GeneratePdf()
{ 
    wrapList = new List<wrapClassPages>();
    pageList = new List<wrapClassPages>();
        for(ApexClass a : [SELECT id, name,createdby.name,createdDate from ApexClass])
        {
            wrapList.add(new wrapClassPages(a, null, false));
        }


   // System.debug('>>>>>WrapList>>>>>>>>>>' +wrapList); 


        for(ApexPage p: [Select id, name, createdby.name, createdDate from ApexPage])
        {
             pageList.add(new wrapClassPages(null, p, false));  
        }


}

public PageReference generate()
{ PageReference pg;
 Document[] docs = new Document[] {};
    //System.debug(wrapList);
    if(wrapList!=null)
    {
        for(wrapClassPages wrp : wrapList)
        {
            if(wrp.ischeck == true)
            {
                System.debug('>>>>>>>>>>>>>wp>>>>>'+wrp.ac.name );
            }
        }
    }
    if(pageList!=null)
    {
       a= new List<String>();
        for(wrapClassPages wrppage : pageList)
        {
            if(wrppage.ischeck == true)
            {
                System.debug('>>>>>>>>>>>>>Pages>>>>>'+wrppage.ap.name );
                a.add(wrppage.ap.name);
                System.debug('>>>>>>>>>>>>> A >>>>>'+a);
                pg = Page.GeneratePDF;
                pg.getParameters().put('name',wrppage.ap.name);
                //docs.add(new Document(body = pg.getcontentAsPDF(),name = wrppage.ap.name + ' PDF',contenttype = 'application/pdf',type = 'pdf'));
             //insert docs;  
            }
        }
    }
    Blob reportPdf;
        try {
        reportPdf = pg.getContentAsPDF();
        system.debug('>>PDF Created'+reportPdf);
       /* Attachment attachmentPDF = new Attachment();
        //attachmentPdf.parentId = a.id;
        attachmentPdf.name = reportPdf + '.pdf';
        attachmentPdf.body = reportPdf.toPdf(pg.getContentAsPDF());
        insert attachmentPDF;*/
    }
    catch (Exception e) {
        reportPdf = Blob.valueOf(e.getMessage());
        system.debug('>>Error creating pdf>>'+reportPdf);
    }

    return null;
}

public class wrapClassPages
{
    public ApexClass ac{get;set;}
    public ApexPage ap{get;set;}
    public Boolean ischeck {get;set;}

    public wrapClassPages(ApexClass ac, ApexPage ap,Boolean v)
    {
        this.ac = ac;
        this.ap = ap;
        ischeck = v;
    }
}
}

I'm able to display list of apex classes and visualforce pages form my org, I want to generate the pdf for selected classes and vf pages. I have trying various method, but unable to get the expected result. I'm not able to generate the PDF.

5
  • You can't use Javascript in any PDF.
    – Adrian Larson
    Jan 31, 2017 at 4:55
  • What is your expected result? Do you want to display a table of all selected classes and pages?
    – Saroj Bera
    Jan 31, 2017 at 5:01
  • 1
    Where is your "GeneratePdf" page? You should create 2 pages one for Visualforce from which you are generating pdf and other is resultant pdf page Jan 31, 2017 at 5:01
  • @Saroj If I try to insert generated pdf in Documents.. the pdf contains all the apex classes and visualforce pages... I'm trying to add only selected classes and visualforce pages in the pdf Jan 31, 2017 at 5:17
  • @Santanu I have added vf page code.. that page is 'GeneratePdf' Jan 31, 2017 at 5:18

1 Answer 1

1

You will need 2 pages here.

Your current page - GeneratePDF is actually a Visualforce page which is showing a list of classes & pages and you have the provision to select/deselect the rows. Now after selecting when you click Generate Pdf button, you need to print only the ones selected. So if you do getContentAsPDF for the same GeneratePDF page again, its going to load the same page again and render as PDF. Your selections will go away, forget about displaying only the selected items.

So you need a second vf page say - GeneratePDFWithSelectedItems, you can keep the same controller as the GeneratePDF page.

So in your GeneratePDFWithSelectedItems page, just loop through your wrapList & pageList and display the items which has ischeck as true. Something like this.

    <apex:repeat value="{!wrapList}" var="wrap">
         <apex:outputPanel rendered="{!wrap.ischeck}">
              {!wrap.ac.Name}<br/>
         </apex:outputPanel>    
  </apex:repeat>

Now in your generate function of the class, just do getContentAsPDF for this 2nd page and save the Blob inside an attachment.

PageReference pg;
    pg = Page.GeneratePDFWithSelectedItems;
     Blob reportPdf;
         reportPdf = pg.getContentAsPDF();
         system.debug('>>PDF Created'+reportPdf);
          Attachment attachmentPDF = new Attachment();
attachmentPdf.parentId = '0019********ABC';//some object id
attachmentPdf.name =  'Somename.pdf';
attachmentPdf.body = reportPdf;
insert attachmentPDF;

You should be good to go.

3
  • thanks for your halp @Santanu but GeneratePDFWithSelectedItems page is blank. I think value for rendered="{!wrap.ischeck}" is changing after loading the page... can you help me here.. Jan 31, 2017 at 9:48
  • No it wont change as long as you have same controller for both the pages. Jan 31, 2017 at 10:00
  • ok..!! no issue, it's working fine Jan 31, 2017 at 10:26

You must log in to answer this question.

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