i've been working on a project trying to produce graphs with chartjs and then printing them using chrome's print to pdf functionality, and i found that the chartjs graphs look poor. after reading various threads both on stackoverflow and github i developed one solution that worked well enough for me.

in my particular case i need the graphs at a fixed size and i can't have them be responsive because i need them to fit within the printed page correctly. i use style tags to set the size:

<canvas style="width: 300px; height: 300px" />

i've found that if you set responsive: false in the chart and then use the style tags like that, chartjs won't mess with the size of the chart. using any other method like setting width or height (not the style width or height) or using css classes will not set it properly. chartjs only seems to work when i set the element's inline style tag for this.

anyway, the trick that worked for me in getting better pdf output was to have chartjs render a larger chart and then scaling it down to a smaller size so it fits on my page correctly.

let's say for some reason we want a 300x300 pixel chart and that it looks poor when we print it to pdf. we need to have chartjs draw this chart into a larger chart and then resize it down to 300x300. in my own project i am having chartjs draw it 2x as large. so for this example i would make a canvas element that is 600x600 as follows:

<canvas style="width: 600px; height: 600px;" class="graph" />

at the same time i have a "graph" css class with height and width set to 300px. the chart will not render at 300px because of the inline style however.

you can then make the chart as you normally would, but immediately after the line of code that makes the chart, you remove the inline style tag from the chart. i found that when you do this, chartjs will draw the chart to the larger 600x600 size but then the chart instantly gets resized to 300x300. here is an example of what the code looks like:

var ctx = canvas.getcontext('2d'); var mychart = new chart( ctx ,{ .... }); canvas.removeattribute("style");

the canvas.removeattribute removes the inline style tag, so then the css class takes effect and instantly causes the canvas to re-render at the smaller size. there is no flash or any indication that this has happened, yet i've found that you get a much higher quality looking chart.

there is one other issue with this. you will have to design your chart for the larger 600x600 size for example in order to get it to look right. when you draw the chart at a larger size, the lines and fonts don't get resized so everything looks really tiny. i had to set my chart manually to the larger size to design it and figure out good fonts and line sizes for the graph first, and then do the resize trick here.

i have also found that simply using the smaller sized chart and making thicker lines or larger fonts does not seem to have the same effect as sizing everything up first, and then rendering it as a smaller size.


since a few versions chart.js has the parameter devicepixelratio. by default, the canvas is rendered in the dpi number by monitor, so 96 or retina - not ideal for a printout, but perfect for the screen.

if you increase this value, more pixels are created. expand the value so that you can export the chart in print quality as a base64 image. the value does not affect the display of the chart on the monitor.

in my case, i set the value to 1.5.

      devicepixelratio: 1.5

documentation: works wonderfully ...


the resolution comes from the canvas size. the more you increase your canvas (width and height), the better will be the resolution when downloading your pdf.

however, you probably don't want to increase the canvas size too much, so, one trick you could use is to create a hidden canvas, with a higher width and height, use it to print the chart and create the pdf, getting a better pdf quality.

here is a fiddle demonstrating this, with an option to download a pdf created from the original canvas/chart, and another option to download a new pdf from the hidden canvas/chart. you can see how the quality increase quite a bit when comparing both results.

i don't think this is the best solution, however, it is the only way i could come up to increase the quality of my pdf chart files. i'm currently playing around both libraries, specially how jspdf treats the w and h arguments when creating the docs.

also, chart.js does come with a built in function to extract an image form the chart (.tobase64image()), however, the quality seems to be worse when i tested.

Related Query

More Query from same tag