How to export a DIV with graphics to PDF

3

Does anyone have any javascript code that exports a graphics div to a .PDF file?

    
asked by anonymous 28.03.2016 / 16:28

1 answer

3

After some time researching and breaking my mind about this issue, I was able to create a script using Chart.js to create a simple chart and then I exported this chart to PDF using jsPDF.js , but to be able to pass the chart to the pdf, I would have to do that before make it into a PNG image, to do this task I used the html2canvas plugin. here's the fiddle with the script I used: link

html:

<canvas id="myChart" width="400" height="400"></canvas>
<button type="button">gerar pdf</button>

js:

 $('button').on('click', function(){
      html2canvas($('#myChart'), {
        onrendered: function(canvas) {
          var imgData = canvas.toDataURL('image/png');
          var pdf = new jsPDF('p', 'mm');
          pdf.addImage(imgData, 'PNG', 10, 10);
          pdf.save('test.pdf');
        }
      });
    });
    
31.03.2016 / 22:03