Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • 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).

like image 633
AnchovyLegend Avatar asked Dec 09 '12 20:12

AnchovyLegend


People also ask

Where is Wkhtmltopdf installed Windows?

When you install wkhtmltopdf, it will go into either c:\Program Files\wkhtmltopdf\ or c:\Program Files (x86)\wkhtmltopdf. 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.


1 Answers

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.

like image 156
Joshua Walcher Avatar answered Oct 03 '22 21:10

Joshua Walcher