Convert HTML to image in php

ramesh picture ramesh · Mar 16, 2012 · Viewed 26.1k times · Source

I have to create a PNG / JPEG image from PHP script .. Briefly

The code will create a html table and will include an image in that page. I need to save this entire page as an image , save that image in my server and return the path of the image ( using webservice ).

I can create image from imagejpg function pretty well . My problem is how to convert that HTML to image, Cant take a screen shot because the processes going on through web services .

Please help me to convert HTML to image using php

Thanks in advance

Answer

Tom picture Tom · Mar 29, 2012

You can download wkhtmltoimage from this link. There is a version for all operating systems so that shouldn't be a problem. Then you can use it like so:

$path="wkhtmltoimg/wkhtmltoimage.exe"; //path to your executable
$url="http://google.com";
$output_path="test.png";
shell_exec("$path $url $output_path");

One thing you want to note is that if PHP is in safe mode, shell_exec will not work and you won't be able to do your conversion.