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:
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:
Use the same page, and via CSS print styles, you can show/hide things that will print only on PDF (as PhantomJs will create PDF in print mode).
Create a custom webpage only for PDF renderization
Source: stackoverflow.com
Related Query
- Chart from chart.js to pdf
- getting additional value fields from data source for dx.chartjs doughnut chart
- Generate PDF from chart and table
- How to clear a chart from a canvas so that hover events cannot be triggered?
- Chartjs random colors for each part of pie chart with data dynamically from database
- How to prevent first/last bars from being cut off in a chart with time scale
- Chart.js Bar Chart - how to chart bars from 0
- Get Chart js object from selector
- How to add datas to chart js from javascript array itself?
- Adding Chart.js line chart to Jinja2/Flask html page from JS file
- How can I remove extra whitespace from the bottom of a line chart in chart.js?
- How to use 'time' (data from database, data type: timestamp ) for plotting graph in Chart JS
- (Vue, ChartJS) Create gradient background for chart from child component canvas context
- In chart.js, Is it possible to hide x-axis label/text of bar chart if accessing from mobile?
- impossible to remove scale from radar chart (chart.js)
- How can I trigger the hover mode from outside the chart with charts.js 2?
- How to start the line graph from the left Y axis in a line/bar mixed chart (Chart.js)?
- Can't size doughnut chart from chart.js
- Using data from API with Chart JS
- Lift the bar up from the bottom in bar type chart
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- Stacked bar chart starting from 0 - ChartJS React
- Chart not displaying from JSON data
- How to remove border from stacked column chart in Chartkick - Chart.js
- How to create chartjs chart with data from database C#
- Draw a horizontal bar chart from right to left
- Redraw Chart.js Chart with Json_encoded array from ajax request
- chart.js add second line in a Line chart that starts from end of first line
- How do I add time sourced from an external source as an X axis to a ChartJS graph?
- Remove padding from chartJs horizontal bar chart
More Query from same tag
- passing each element of array in the labels of char.js
- TypeError: Cannot assign to read only property ... - Editing react-chartjs-2 datasets with Redux
- Chart.js - Bar values showing on the middle of bar
- Redraw Chart.js Chart with Json_encoded array from ajax request
- Legends for line charts in Chart.js
- Chart.js - if there is not value show 0
- Refresh chart.config.data
- chartjs 2.7 how to add labels in center of horizontal bar graph
- Chart JS — Conditional horizontal row background colours
- Can I add a callback function to the legend while keeping the original functionality?
- How to set a global chart.js tooltip label callback
- what is wrong with my code ? java script chart position
- I can't add data to chartjs from codeigniter
- Is it possible to dynamically add more Y Axes or more X Axes on chart js?
- Chartjs in format Line is full points
- Chart js cut the title and the legends
- Chart js Backgroundimage Scaling
- ChartJs 2.0 How do I obtain point information being clicked upon?
- Alter angular js chart wrapper to support drawing custom lines
- Draw stacked horizontal bar chart with Average line using ChartJS
- How can I achieve displaying data points on chart but Dotted? Chart.js V3.7.0
- Chartjs: Rotate Axis Title
- How to set X coordinate for each bar with react chart js 2?
- Chartjs - first x-axis tick is not displayed
- Setting specific color per label in chart.js
- Hiden Gridline in chart JS
- Formatting x-axis labels as time values in Chart.js
- chart js different callback labels
- Chart.js issue in plotting numeric X and Y in line chart
- How to set defaults for scales in ChartJS?