while this does not answer the question directly as i could not get chart.js to do what i wanted being client side, i found a good solution using pchart. as the pdf is being generated, the chart is created and is temporarily saved on the server until the pdf is finished. the chart is inserted into the pdf and then deletes the chart from the server. there is a bit of manipulation to correctly size the image.


if (file_exists("path/imagename.png")) {
    $ycurrent = $pdf->gety();
    list($width, $height) = getimagesize("path/imagename.png");
    $imagewidth = 160;
    $ratio = $imagewidth/$width;
    $xpos = 25;
    $pdf->image("path/imagename.png", $xpos, $ycurrent, $width * $ratio, $height * $ratio, "png", '');
    unlink ("path/imagename.png");


i was working on a similar problem and built quickchart. it is a web service that renders chart.js charts in several static formats including pdf.

the full source code is here:

you may be interested in lib/charts.js and lib/pdf.js in particular. the solution is built on top of a javascript canvas implementation (node-canvas) and a chart.js wrapper library (chartjs-node-canvas). first the chart is rendered to a png image using the canvas. then the png is positioned on a document and rendered using pdfkit.

hope you find the source code useful. i also host the quickchart web service for free so you can use the web api directly{chart.js config here}

here's an example of a pdf chart generated on-the-fly:{type:'bar',data:{labels:['january','february','march','april','may'],datasets:[{label:'dogs',data:[50,60,70,180,190]},{label:'cats',data:[100,200,300,400,500]}]}}


this is fine but what if you want to bypass the on screen part and go straight to a pdf document and include the image

you can still use chart.js exporting as pdf with phantomjs (a headless browser that will open a html page with your charts, and save it via pdf). if you are working with nodejs there's a good library that abstracts phantomjs and make the pdf easy: it seems like a workaround, but usually these canvas to pdf doesn't render well, specially charts!

yeah, you still need to create a html page to print your pdf, however you have 2 options:

Related Query

More Query from same tag