Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save npenalopez/c15188c9e826175bf5a8dfd73c76c424 to your computer and use it in GitHub Desktop.
Save npenalopez/c15188c9e826175bf5a8dfd73c76c424 to your computer and use it in GitHub Desktop.
Chart.js - Download Multiple Charts as PDF
<a href="#" id="downloadPdf">Download Report Page as PDF</a>
<br/><br/>
<div id="reportPage">
<div id="chartContainer" style="width: 30%;float: left;">
<canvas id="myChart"></canvas>
</div>
<div style="width: 30%; float: left;">
<canvas id="myChart2"></canvas>
</div>
<br/><br/><br/>
<div style="width: 30%; height: 400px; clear: both;">
<canvas id="myChart3" style="width: 40%"></canvas>
</div>
</div>
var chartColors = {
red: 'rgb(255, 99, 132)',
orange: 'rgb(255, 159, 64)',
yellow: 'rgb(255, 205, 86)',
green: 'rgb(75, 192, 192)',
blue: 'rgb(54, 162, 235)',
purple: 'rgb(153, 102, 255)',
grey: 'rgb(231,233,237)'
};
var randomScalingFactor = function() {
return (Math.random() > 0.5 ? 1.0 : 1.0) * Math.round(Math.random() * 100);
};
var data = {
labels: ["Car", "Bike", "Walking"],
datasets: [{
label: 'Fuel',
backgroundColor: [
chartColors.red,
chartColors.blue,
chartColors.yellow],
data: [
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
]
}]
};
var myBar = new Chart(document.getElementById("myChart"), {
type: 'horizontalBar',
data: data,
options: {
responsive: true,
title: {
display: true,
text: "Chart.js - Base Example"
},
tooltips: {
mode: 'index',
intersect: false
},
legend: {
display: false,
},
scales: {
xAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
var myBar2 = new Chart(document.getElementById("myChart2"), {
type: 'horizontalBar',
data: data,
options: {
responsive: true,
title: {
display: true,
text: "Chart.js - Changing X Axis Step Size"
},
tooltips: {
mode: 'index',
intersect: false
},
legend: {
display: false,
},
scales: {
xAxes: [{
ticks: {
beginAtZero: true,
stepSize: 2
}
}]
}
}
});
var myBar3 = new Chart(document.getElementById("myChart3"), {
type: 'horizontalBar',
data: data,
options: {
responsive: true,
maintainAspectRatio: false,
title: {
display: true,
text: "Chart.js - Setting maintainAspectRatio = false and Setting Parent Width/Height"
},
tooltips: {
mode: 'index',
intersect: false
},
legend: {
display: false,
},
scales: {
xAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
$('#downloadPdf').click(function(event) {
// get size of report page
var reportPageHeight = $('#reportPage').innerHeight();
var reportPageWidth = $('#reportPage').innerWidth();
// create a new canvas object that we will populate with all other canvas objects
var pdfCanvas = $('<canvas />').attr({
id: "canvaspdf",
width: reportPageWidth,
height: reportPageHeight
});
// keep track canvas position
var pdfctx = $(pdfCanvas)[0].getContext('2d');
var pdfctxX = 0;
var pdfctxY = 0;
var buffer = 100;
// for each chart.js chart
$("canvas").each(function(index) {
// get the chart height/width
var canvasHeight = $(this).innerHeight();
var canvasWidth = $(this).innerWidth();
// draw the chart into the new canvas
pdfctx.drawImage($(this)[0], pdfctxX, pdfctxY, canvasWidth, canvasHeight);
pdfctxX += canvasWidth + buffer;
// our report page is in a grid pattern so replicate that in the new canvas
if (index % 2 === 1) {
pdfctxX = 0;
pdfctxY += canvasHeight + buffer;
}
});
// create new pdf and add our new canvas as an image
var pdf = new jsPDF('l', 'pt', [reportPageWidth, reportPageHeight]);
pdf.addImage($(pdfCanvas)[0], 'PNG', 0, 0);
// download the pdf
pdf.save('filename.pdf');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.3/jspdf.debug.js"></script>
#chartContainer {
width: 40%
};
#myChart2 {
width: 40%
};
@septiadirahmawan
Copy link

good

@emanresuwenaretne
Copy link

Thanks for the implementation.

Few questions

1)Is there any way to increase the Width of the JSPDF document? I tried to multiply reportPageWidth * 4 but that doesn't seem to work. My graphs are shown chopped off.
2)How do I add non-canvas elements from the page to the export? Thanks for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment