AnchovyLegend
AnchovyLegend

Reputation: 12538

Using wkhtmltopdf on Windows

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:

Upvotes: 12

Views: 49092

Answers (2)

Joshua Walcher
Joshua Walcher

Reputation: 496

I realize that this is an old question, but since it is the only library I've found that will print from HTML to PDF and retain images and background colors, I use the Windows Command Prompt. I used to use PDF Architect, but it never was able to handle background colors.

When you install wkhtmltopdf, it will go into either c:\Program Files\wkhtmltopdf\ or c:\Program Files (x86)\wkhtmltopdf.

Let's say that you have a local PHP environment where http://localhost/ is set up as your starting point. Let's also say that you have a file there called html_invoice.html and that you want to print it to a file called dev_invoice_200.pdf and put it in your invoices folder inside the xampp web server. To do that, you would fire up the Windows command prompt (i.e. Start -> All Programs -> Accessories -> Command Prompt or Start -> Run -> cmd) and type:

c:\Program Files\wkhtmltopdf\bin\wkhtmltopdf http://localhost/html_invoice.html c:\xampp\htdocs\invoices\dev_invoice_200.pdf

This will create the PDF in the right place.

Since we're using the Windows command prompt, there's really no reason to use PHP. Instead, we can use a batch file, and if we need to automate it, we can use Windows Task Scheduler to call the batch file (Start -> All Programs -> Accessories -> System Tools -> Task Scheduler).

If you've never done batch files, you just type out the commands you want to do into a plain text file and save it with a .bat extension.

So let's say that we want to always print the file html_invoice.html at that same spot into the invoices folder with a name of dev_invoice.pdf and that we're going to change the name after it gets there. You'd just put the line above minus the "_200" into a text file and save it as .bat and then double-click on it in Windows or run it through Task Scheduler.

Upvotes: 12

Joel Peltonen
Joel Peltonen

Reputation: 13402

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.

Upvotes: 10

Related Questions