Render as PDF in Visualforce (Salesforce)
Apex Class Code :
public class PdfRender{
public Account acc {get;set;}
public List<Account> ListAcc {get;set;}
public PdfRender(){
ListAcc = new List<Account>();
acc = new Account();
ListAcc =[Select Name,AccountNumber from Account Limit 10 ];
}
// return 'pdf' if the parameter 'p' has been passed to the page, or null
public String getSelectRender() {
if(ApexPages.currentPage().getParameters().get('p') != null)
return 'pdf';
else
return null;
}
public PageReference RenderedAsPDF() {
PageReference pdf = Page.RenderedAsPdf;
pdf.getParameters().put('p','p');
return pdf;
}
}
public Account acc {get;set;}
public List<Account> ListAcc {get;set;}
public PdfRender(){
ListAcc = new List<Account>();
acc = new Account();
ListAcc =[Select Name,AccountNumber from Account Limit 10 ];
}
// return 'pdf' if the parameter 'p' has been passed to the page, or null
public String getSelectRender() {
if(ApexPages.currentPage().getParameters().get('p') != null)
return 'pdf';
else
return null;
}
public PageReference RenderedAsPDF() {
PageReference pdf = Page.RenderedAsPdf;
pdf.getParameters().put('p','p');
return pdf;
}
}
VF Code :
<apex:page renderAs="{!SelectRender}" controller="PdfRender" >
<apex:pageBlock title="Demo Pdf View">
<apex:pageBlockSection >
<apex:pageBlockTable value="{!ListAcc}" var="ac">
<apex:column value="{!ac.name}"/>
<apex:column value="{!ac.AccountNumber}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:form >
<apex:commandButton action="{!RenderedAsPDF}" value="PDF View" id="theButton"/>
</apex:form>
</apex:page>
<apex:pageBlockSection >
<apex:pageBlockTable value="{!ListAcc}" var="ac">
<apex:column value="{!ac.name}"/>
<apex:column value="{!ac.AccountNumber}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:form >
<apex:commandButton action="{!RenderedAsPDF}" value="PDF View" id="theButton"/>
</apex:form>
</apex:page>
No comments:
Post a Comment