Wednesday, 29 January 2014

Print VF page on button click

VF page Code
============

<apex:page controller="bhadraPieChartController" title="Pie Chart" showHeader="false">
<script>
   //this is only the code for printing the page
    function fun(){
           document.getElementById('prnBtn').style.display='none'; 
           //document.getElementById('winBtn').style.display='none';    
           window.print();
            document.getElementById('prnBtn').style.display='block'; 
    }
</script>
<apex:form >
  <div  style="padding-left: 484px;padding-top: 27px;"><input type="button"   onClick="fun();" id="prnBtn"  value="Print This Page" style="background: green;"/></div>
</apex:form>
    <apex:chart height="350" width="450" data="{!pieData}">
        <apex:pieSeries dataField="data" labelField="name"/>
        <apex:legend position="right"/>
    </apex:chart>
</apex:page>
==========
Controller
============

public class bhadraPieChartController {


  public List<BhadraPieWedgeData> getPieData() {

       List<BhadraPieWedgeData> namedata = new List<BhadraPieWedgeData>();
 

    namedata.add(new BhadraPieWedgeData('Jan', 30));
     
    namedata.add(new BhadraPieWedgeData('Feb', 15));
     
    namedata.add(new BhadraPieWedgeData('Mar', 10));
     
    namedata.add(new BhadraPieWedgeData('Apr', 20));
     
    namedata.add(new BhadraPieWedgeData('May', 20));
     
    namedata.add(new BhadraPieWedgeData('Jun', 5));
     
    return namedata;
 
  }
 
 
 
 
    public class BhadraPieWedgeData {

     
        public String name { get; set; }
     
        public Integer data { get; set; }
     
        public BhadraPieWedgeData(String name1, Integer data1) {
               this.name = name1;          
               this.data = data1;
        }

 
    }

}
=============
Output:
==========

Print page

No comments:

Post a Comment