0

I have PHP code that grabs some data from a server and then uses it to construct a PDF. As part of the PDF, I'd like to include a google chart. However, google chart is Javascript. Since I'm relatively new to web programming, can anyone provide suggestions on how I can incorporate google charts into a PHP script. Note, the data that the PHP script retrieves will be needed to construct the chart, so if it is a separate Javascript file or something, then it will need to somehow pass in data.

Thanks in advance!

CodeGuy
  • 28,427
  • 76
  • 200
  • 317
  • i wouldn't use a google cart, i would use a chart built by something that provides an image, sit it can easily be embedded in a pdf –  Dec 11 '14 at 21:54
  • exactly, you need a charting system that generates an image not a bunch of javascript code that needs a browser to display it – developerwjk Dec 11 '14 at 21:55
  • stuff like html2canvas might be of use. – Dimitri Dec 11 '14 at 21:55
  • [Duplicate](http://stackoverflow.com/questions/14844629/php-google-charts-api). Essentially, you want the [Google Image Chart API](https://developers.google.com/chart/image/) or you want to host the chart somewhere else and then use a PHP library to grab a screenshot and edit it (ImageMagick and GD are good places to start). – beardedlinuxgeek Dec 11 '14 at 21:56
  • @beardedlinuxgeek but it's deprecated... – CodeGuy Dec 12 '14 at 04:00
  • @CodeGuy but still supported until April 2015. If you're not going to need your program after that then use Google's API. Otherwise I said you need to host the chart and use something like [URL2PNG](https://www.url2png.com/) to take a screenshot and the manipulate it in PHP with ImageMagick or GD. – beardedlinuxgeek Dec 12 '14 at 05:02

1 Answers1

0

The google chart is a javascript object. You can send the object to your PHP script via AJAX. Then json_decode(obj) in your php script, which will turn your javascript object into a PHP stdClass Object. You can then incorporate the information from your script into the object, json_encode() back into a json object and echo the response back to your page to update the view.

mstewart
  • 66
  • 5
  • and where is the image for the pdf? –  Dec 11 '14 at 22:12
  • @mstewart thanks for your answer however I guess I still don't understand how this would generate an image that I can use. Could you give a complete example? – CodeGuy Dec 12 '14 at 04:02