FPDF: Failing to Insert a Google Chart into a PDF CodeUnit 13 OCT 2010

fpdf-logoMy tried and trusted method for inserting graphs into PDFs is to use the nifty PHP FPDF library and insert an image into it via a Google Chart URL.

In other words, your code would look something like this:

$charturl = "http://chart.apis.google.com/chart?cht=lc&chco=" . implode(',', $colors) . "&chs=930x310&chd=t:" . implode('|', $graphdata) . "&chxt=x,y&chxl=0:|" . implode('|', $monthsheading) . "|1:|10|20|30|40|50|60|70|80|90|100";
$pdf->Image($charturl, null, null, 180, 60, 'PNG');

99% of the time though, my PDF fails, gives me an error about not being able to load the image and leaves me in tears.

Because each and every time, I can without fail go look at the PHP error log and will be greeted with this:

failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request

And then I remember: URLENCODE the damn graph string before feeding it into the Image function!

One day I’ll remember to do this from the get go, promise! :)

Related Link: http://php.net/manual/en/function.urlencode.php

Related Posts:

About Craig Lotter

South African software architect and developer at Touchwork. Husband to a cupcake baker and father to two little girls. I don't have time for myself any more.

  • Max

    Were you ever able to put these api charts into a pdf?

    • Yes I did, but it involves a bit of trickery, namely saving the google chart image to the server and then including the saved image into fpdf the standard way one would include images.