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.