How to export a DIV with graphics to PDF

Asked

Viewed 1,716 times

3

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

  • as far as I know, and with my previous experiences in this, I can only do this with PHP!

  • if you know how to use it see this: http://pdfkit.org/

  • Vlw by the answer @Leandroruel, just a doubt, this pdfkit can create a pdf from a DIV? why the examples he has on this site he creates everything in hand.

  • opa @sergioBertolazo not tested just searched something for you to take a look, have to read the documentation better, if not meet your need, search for "javascript pdf Generator"

  • It’s expensive I tested no function msm.. @Leandroruel , vlw by help ai guy will have to continue the search to do

  • friend, which graphic plugin is using in html page?

  • @Leandroruel I’m using Chart.js

  • Have you tried using the fpdf ?

Show 4 more comments

1 answer

3


after a while researching and cracking my head on this issue, I was able to create a script using Chart js. to create a simple chart and then export this chart to PDF using jsPDF.js, but to be possible to pass the graphic to pdf, I would have to first transform it into an image in PNG format, to accomplish this task I used the plugin html2canvas. here’s the fiddle with the script I used: https://jsfiddle.net/tj9tsyc3/5/

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');
        }
      });
    });
  • 1

    Thanks for the reply @Leandro Ruel, it worked perfectly for me, only in Google Chrome, Firefox, IE 10 e 11, IE 9 is giving error, I will try to find a way to get, thanks for the help brother!

  • arrange! anything just comment here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.