Using wkhtmltopdf on Windows

AnchovyLegend picture AnchovyLegend · Dec 9, 2012 · Viewed 29.5k times · Source

I am trying to set up the nifty HTML to image plugin called wkhtmltopdf and I am having a really difficult time.

What I did so far:

  1. Downloaded wkhtmltopdf zip package and upacked the file in my websites root folder

  2. As a test I included the following code in my index.php file, which I would expect to save the contents of bbc.com website as a .jpg image in my websites root folder: ..

    shell_exec('./wkhtmltopdf --quality 50 http://www.bbc.com bbc.jpg');

Nothing seems to happen. PHP is not running in safe mode, so that isn't the problem. shell_exec is executing, I was able to create a .txt document using it.

I have been researching this for days, I am offering 100 bounty to anyone that can come up with with a clear, simplified step-by-step working guide on how to set up wkhtmltopdf on Widows to run using PHP.

More specifically I am looking for simplified walk-through on how to:

  • Set up wkhtmltopdf to run on Windows using php (include details on common problems and how to overcome them).

  • On click of a button or link: print the 'current' web page to pdf, including all filled out input values (include sample code for this part). Webpages with events that change the content of the page, should print out the current version of the page, not the initial state of the page.

  • Create a download link of the .pdf created, onClick of the same function mentioned above that creates and prints to pdf (include sample code for this part).

Answer

Joel Peltonen picture Joel Peltonen · Dec 16, 2012

It's kind of hard to make out what is the actual issue here but I made a blag post tutorial on getting wkhtmltopdf to respond in a Windows 7 / PHP Environment. Ask away if you need further details - I'm sure that this does not cover your issue.

Like I said in the comments as well, your function call shell_exec('./wkhtmltopdf --quality 50 http://www.bbc.com bbc.jpg'); will fail because wkhtmltopdf and wkhtmltoimage are different programs and accept different command line options. Replace wkhtmltopdf with wkhtmltoimage in that command and you might get an output file generated. I believe that in your case a test execution like shell_exec('c:\wkhtmltopdf\wkhtmltopdf www.google.com goog.pdf 2>> err3.txt 1>> out3.txt'); would be most interesting as there you can see all the different input/output that wkhtmltopdf does.