score:26
Honestly, it seems like the easiest approach would be to just provide a "download to PDF" link that pops up the browser's print page and instruct to user to select "print as pdf".
If that approach doesn't work for you (or your users), then here is a rough way to do it.
Basically, we create a new canvas
element that is the size of your report page and incrementally paint the pixels from your existing chart.js canvas
charts into the new canvas
. Once that is done, then you can use jsPDF
to add the new canvas to a pdf document as an image and download the file.
Here is an example implementation that does just that.
$('#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');
});
You can see it in action at this codepen.
Now let's talk about some gotchas with this approach. First, you have to control the position of each chart.js canvas
in the new canvas
object. The only way to do that is to have an understanding of how your report page is structured and implement that same structure. In my example, my charts are in a 2x2 grid and the logic handles this accordingly. If you had a 3x2 grid or something different then you would have to change the positioning logic.
Lastly, the final pdf output file dimensions are much larger than the original chart page (from the web). I think the reason is because my chart "container" div stretches across the full page. Therefore, you probably want to use a different approach for setting the size of your new canvas
.
So long story short, the above example is meant to demonstrate an approach and not be your final solution.
Good luck!
score:1
I have a working solution in vanilla javascript(although I used ts typing) and using the lib jsPdf, where you need a plot per pdf page:
let index = 1;
// create new pdf object
// if don't choose compress as true you will end up with a large pdf file
let pdf = new jsPDF({
orientation: 'landscape',
unit: 'px',
format: 'a4',
compress: true,
})
// search for the html element(s) you need
const canvas = document.querySelectorAll("canvas");
// here my size are in pixels since I configured that in the obj instance
let pageWidth = 400;
let pageHeight = 400;
let index = 1;
// traverse the array of canvas
canvas.forEach( (canva:HTMLCanvasElement) => {
// I added some options among others I added the type of the compression
// method: FAST
pdf.addImage(canva, 'PNG', 10, 10, pageWidth, pageHeight, `img${index}`, "FAST");
// so as to not end up with an extra pdf page at the end of the iteration
if (index < canvas.length) {
pdf.addPage();
}
index++;
});
// download the pdf
pdf.save('Reporte.pdf');
Source: stackoverflow.com
Related Query
- Page with multiple chart.js charts to pdf
- Multiple charts in one page with chart.js
- Chart.js - loading multiple charts with draggable datapoints into a single HTML Page
- multiple charts js in page but with same tooltips (about last chart)
- Draw multiple chart in one page with Chart.js
- Want to multiple charts on same page with different data
- How to render multiple Chart.JS charts on the same page with AJAX data and variables
- Chart changes different on zoom when using multiple charts with chart.js and flask
- How do I create multiple charts at the same page with angular?
- how to not repeat code while creating multiple charts in chart js
- ChartJS - Donut charts with multiple rings
- Chart.js - Draw bar chart with multiple labels
- Chart.js Multiple charts with one common legend
- Multiple charts on same page not working - ng2-charts
- Chart.js - Creating multiple charts on one page
- Group Chart with multiple layers in ChartJS
- Multipe doughnut charts on one page with text in center using Chart.js
- Dynamicly update scatter/line chart in Chart.JS with multiple x/y grids
- Angular-chart / line chart with multiple horizontal lines (margins)
- Vue Chart 3 - Doughnut Charts with Text in the Middle (Trouble registering a plugin)
- react-chartjs-2 line chart with time on X-axes with multiple data sets plotted wrong
- Impossible to render multiple charts with chart.js
- How can I load multiple Chartjs charts with different data on the same page?
- ChartJS update chart with multiple datasets
- Generate multiple line charts in Django with ChartJS
- ChartJs - Round borders on a doughnut chart with multiple datasets
- how to display multiple sum with chart js and laravel?
- Chart.js -> line chart -> multiple points with the same X
- How to map multiple charts with chart.js in react
- React with chart js, labelling problems in multiple datasets for doughnut chart
More Query from same tag
- Copy div content to dynamically create a modal window:possible?
- Adding trendlines to existing chart Chart.js
- Use data from SQL database in .js file with PHP
- how to set a custom tick format in chartjs options from laravel controller?
- Why is chartjs treating these arrays differently?
- chartjs radar glowing edges effect
- Am I missing a chart.js component or helper?
- Query with empty results laravel eloquent
- javascript chart library for every minutes data
- ChartJs + ie11 doesn't work
- Chart.js Radial axes: is it possible to use multiple axes on radar chart?
- Vue.js - this.<value>.<value> is undefined
- How to wrangle JSON data into arrays of unknown name
- Vuejs with ChartJS populate from API
- Chartjs barchart group dataset by label
- JS node chart from SQL Server not updated
- Synchronizing zoom between multiple charts
- Chartjs : Remove specific labels
- Cannot read property 'length' of null error in Javascript
- Chart.js - Uncaught ReferenceError: chart is not defined
- How write the labels of the data inside a Doughnut Chart made with Chart.js?
- Populating Chart.Js line graph with multiple lines using data from a JSON Object
- Chartjs linechart with only one point - how to center
- How could chartjs font family display everywhere
- Chartjs isn't rendering bar chart, even though chart variable shows correctly when console logged
- Chartjs - How to get last 7 days on x-axis labels?
- How to show data values in top of bar chart and line chart in chart.js 3
- Chart js x-axis values getting repeated twice
- Split JSON into two arrays gives undefined keys-values
- ChartJS: How to set fixed Y axis max and min