12

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

AnchovyLegend
  • 12,139
  • 38
  • 147
  • 231
  • What happens when you invoke the program at the command line, outside of PHP? Does it work? – Charles Dec 09 '12 at 20:05
  • I am running the linux command in the php script on a Windows so I am not sure if/how I would be able to run it from the command line directly. – AnchovyLegend Dec 09 '12 at 20:07
  • SSH into the server and try out, then? You might need to check with your host to see if they support SSH. They likely have a small tutorial on doing so as well. Chances are that this is something simple, like bad permissions, but it'll be easiest to find out at the command line at the server itself. – Charles Dec 09 '12 at 20:10
  • OK, I found out the that shell_exec command actually works, by running this script: shell_exec('dir > dir.txt'); , so its a command syntax issue, any ideas? – AnchovyLegend Dec 09 '12 at 20:12
  • `shell_exec` might work for you for *other* commands, but we won't find out what's wrong with the `wkhtmltoimage` command without trying it out directly. The syntax you used in your question is fine. – Charles Dec 09 '12 at 20:13
  • if you are offering the bounty, offer the bounty. click "start a bounty" – Janus Troelsen Dec 12 '12 at 16:57
  • remember to turn on error reporting in PHP. please confirm that you have error reporting set to E_ALL or more. – Janus Troelsen Dec 12 '12 at 16:58
  • Afaik... `wkhtmltopdf` requires an `X` server.... I usually use something like `xvfb-run -a -s "-screen 0 800x600x25" wkhtmltopdf`... but that's on (headless) linux... – Wrikken Dec 12 '12 at 17:15
  • `wkhtmlto*` can work without X as well, but the result might not be as good. What does it vomit out with `shell_exec('./wkhtmltopdf --version 2>> error.txt 1>> output.txt');`? – Joel Peltonen Dec 13 '12 at 14:57
  • Another comment: `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 -- I suspect this is just a paste bug so I won't add as answer unless it actually works :) – Joel Peltonen Dec 13 '12 at 15:03
  • If those are your real requirements, wkhtmltopdf is not a good fit - and I don't know of any system that would support something like this. This is a massive project and may be possible with wkhtmltopdf too, but require weeks of coding with full repo access. All code examples would depend on your unique page structure. Like "should print out the current version of the page" for example varies MASSIVELY on how to do this, like do you use Backbone.js or what for the view and do you support viewstate linking and such. Maybe try PhantomJS or html2canvas instead. – Joel Peltonen Sep 30 '13 at 06:57

2 Answers2

11

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.

Joshua Walcher
  • 506
  • 4
  • 14
10

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.

Joel Peltonen
  • 13,025
  • 6
  • 64
  • 100