Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

Export to PDF support

Open pinara747 opened this issue 13 years ago • 8 comments

It would be really helpful to give users the option to be able to export the graphs to PDF. Is there any way to do this right now?

pinara747 avatar May 11 '13 09:05 pinara747

PDF support isn't really within the scope of Chart.js, but you can export your canvas to a base64 string as an image, which you can do what you will with.

//Where ctx is the 2d context of your canvas.
//Returns a base64 string of the canvas image
ctx.canvas.toDataURL()

nnnick avatar May 13 '13 17:05 nnnick

@nnnick when I call "toDataURL()" the chart isn't full rendered yet, so the base64 string is incomplete. Is there any way to turn around this problem?

Follow below my code:

new Chart(document.getElementById("canvas").getContext("2d")).Bar(barChartData);
document.getElementById("chart_image").src = document.getElementById("canvas").toDataURL();

FabricioFFC avatar Sep 25 '13 02:09 FabricioFFC

Any news regarding this aspect ? I'm also looking for a way to save chart.js generated canvas as image to embed them later in a pdf file ....

ANy clue would be very appreciated. Thanks

simogeo avatar May 12 '14 10:05 simogeo

For information, the use of onAnimationComplete is needed

See : http://jsfiddle.net/AbdiasSoftware/KSgV7/1/

simogeo avatar May 12 '14 11:05 simogeo

@FabricioFFC @simogeo

Hi there. Could you use the approach to put the data in pdf? Thanks.

metaxos avatar Feb 19 '15 13:02 metaxos

Here is what I'm using to convert the toDataURL() to a png image and save on my server, if it is of any help: PHP code

    // Interpret data uri
    $uriPhp = 'data://' . substr($file, 5);
    // Get content
    $binary = file_get_contents($uriPhp);

    $file = 'uploads/charts/'. time() .'.png';

    // Save image
    file_put_contents($file, $binary);

Reference: http://stackoverflow.com/a/6735458/4575150

DiogoAbu avatar Feb 21 '15 07:02 DiogoAbu

@metaxos @FabricioFFC : more or less the same here ...

simogeo avatar Feb 23 '15 13:02 simogeo

Proper way is to use toDataURL method to get the chart image data and then add them to a pdf file. However when everything fails, a lazy but effective solution is to render your content on client side and use a combination of javascript utilities to screenshot desired chart(s) (ex: html2canvas.js) and generate a pdf out of them (ex: jsPdf.js)

raminv80 avatar Mar 09 '16 05:03 raminv80