score:0

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.

$mypicture->render("path/imagename.png");

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");
}

score:2

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: https://github.com/typpo/quickchart

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 https://quickchart.io/chart?width=500&height=300&format=pdf&c={chart.js config here}

here's an example of a pdf chart generated on-the-fly:

https://quickchart.io/chart?f=pdf&bkg=white&c={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]}]}}

score:4

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: https://github.com/sindresorhus/pageres. 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