jaroApp
jaroApp

Reputation: 899

Google Charts export as image

I'd like to get simple pie google chart (like this one https://developers.google.com/chart/interactive/docs/gallery/piechart) and set to PDF as image.

To create PDF I use laravel-dompdf, which is a laravel wrapper for DOMPDF library. This library doesn't support javascript.

It should be in background. I mean that I don't have step where I display html in browser to save charts as image from javascript.

Any hints? It's also possible to use different similar library but I could not found nothing interesting.

Thanks in advance.

Upvotes: 2

Views: 4499

Answers (1)

Bakayaro
Bakayaro

Reputation: 130

Google offers a function called getImageURI() for its charts. This will return the chart as an image but in base64 encoded format. It's described under https://developers.google.com/chart/interactive/docs/printing. Using this and a headless broswer such as PhantomJS you can get that base64 encoded image.

You can use https://github.com/jonnnnyw/php-phantomjs to load the webpage with the chart on it and then fetch the base64 encoded image from that website.

I would implement a javascript function that posts the base64 string to an endpoint and then you will be able to save that as a pdf/png/jpg or in any other formats.

I hope this helps.

Upvotes: 3

Related Questions