jsPDF Chart.js Example to Add Colorful Barcharts inside PDF Document Using jQuery in Browser Using HTML5 & Javascript Full Project For Beginners

 

 

index.html

 

 

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://code.jquery.com/jquery-2.2.4.js"></script> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.2/Chart.min.js"></script> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.2.61/jspdf.debug.js"></script> 
      <style id="compiled-css" type="text/css">

.container {//from w ww . j  a  v  a 2s .  co  m
   width: 80%;
   margin: 15px auto;
}


      </style> 
      <script type="text/javascript">
    window.onload=function(){
$(document).ready(function(){
$('#hyppdf').click(function(){
    var canvasImg = document.getElementById("myChart").toDataURL("image/png", 1.0);
    var doc = new jsPDF();
    doc.setFontSize(33);
    doc.setFillColor(204, 204,204,0);
    doc.rect(10, 10, 150, 160, "F");
    doc.addImage(canvasImg, 'png', 10, 10, 150, 100);
    doc.save('sample.pdf');
})
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: ["M", "T", "W", "T", "F", "S", "S"],
    datasets: [{
      label: 'apples',
      data: [12, 19, 3, 17, 28, 24, 7],
      backgroundColor: "rgba(255,255,255,1)"
    }, {
      label: 'oranges',
      data: [30, 29, 5, 5, 20, 3, 10],
      backgroundColor: "rgba(255,153,0,1)"
    }]
  }
});
})
    }

      </script> 
   </head> 
   <body> 
      <div class="container" style="background-color:#ccc"> 
         <h2> 
            <a id="hyppdf" href="#">download</a>
         </h2> 
         <div> 
            <canvas id="myChart"></canvas> 
         </div> 
      </div>  
   </body>
</html>

Leave a Reply