Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating a line chart using Morris.js which the data are retrieved from mySQL database. I wanted to download it as PDF file that contains the entire chart.

Here's my code :

<?php 
      $connect = mysqli_connect("localhost", "root", "", "testing");
      $query = "SELECT * FROM purchase";
      $result = mysqli_query($connect, $query);
      $chart_data = '';
      while($row = mysqli_fetch_array($result))
      {
        $chart_data .= "{ year:'".$row["year"]."', profit:".$row["profit"].", purchase:".$row["purchase"].", sale:".$row["sale"]."}, ";
      }
      $chart_data = substr($chart_data, 0, -2);
    ?>

<div class="row">
       <div id="chart" style="width:800px;"></div>
       <canvas id="canvas" width="800px;"></canvas>
       <button onclick="save();">Download</button>  
    </div>

Morris.Line({
     element : 'chart',
     data:[<?php echo $chart_data; ?>],
     xkey:'year',
     ykeys:['profit', 'sale'],
     labels:['Profit', 'Sale'],
     hideHover:'auto',
     stacked:true
   });


   function save() {
      html2canvas(document.getElementById('canvas'), {
      onrendered: function(canvas) {
      var img = canvas.toDataURL();
      var doc = new jsPDF();
      doc.addImage(img, 10, 10);
      doc.save('test.pdf');
        }
     });
   }


What I have tried:

I've tried using html2canvas and jsPDF to generate the PDF. However, the PDF generated/downloaded does not contain any data on it.
Posted
Comments
[no name] 7-May-19 11:32am    
Try "printing"; select a PDF (or XPS, etc.) printer driver when it comes time to select a printer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900