I'd like to generate a pdf of my chart's output. I'm using the google library.
I've tried a standalone example which works, where the image is saved during the google lib generation process and retrieved later for inclusion in pdf generation using mpdf (https://stackoverflow.com/questions/46417248/how-to-export-google-chart-...)
This relies on google.visualization.events.addListener() acting on the chart object (eg mychart) and returning mychart.getImageURI() (effectively the chart image) and storing in a hidden div etc for later retrieval etc.
To follow the example's first step this needs a ref to the chart object and access to the google library object.
Any hints on how we could do something similar, eg write custom js to collect the image, which could be AJAXed back to the server.
Comments
Comment #2
jons commentedOK It is possible to patch charts/modules/charts_google/js/charts_google.js - Drupal.googleCharts.drawChart to capture the chart image as a encoded png (src in the form: 'data:image/png;base64,iVBORw0KGgoAAAAN...') with:
then send this to server via AJAX and render into a real png using DOM, then include in the pdf created with mpdf (https://mpdf.github.io).
A better approach is to write your own script which collects the SVG HTML and send to server. This can be converted into pdf directly by mpdf. The only drawback is that you have to include a timer callback in the js to check when the svg has been created by the charting js.
The SVG does include the data to support the hover behaviour to show data point details. It would make sense to strip this out so an instance does not 'leak through' in the pdf.
Comment #3
andileco commentedGoing to close this. If other people find this ticket, they may be interested in this approach too: https://www.drupal.org/project/charts/issues/3197574#comment-13999765