Installing WKHTMLTPDF on Windows with XAMPP PHP Example

WKHTMLTOPDF is a fantastic library that allows you to convert your HTML page to a PDF using PHP’s shell_cmd() or exec() functionality. Personally, I tend to always use shell_exec with WKHTMLTOPDF in PHP. When using XAMPP, the combination is a pretty painless installation. If you haven’t already downloaded and installed the software, do so now. My installation is stored in “C:\xampp” to keep things nice and neat. I tend to always stay away from the Program Files directory because of permission issues.

Since this tutorial and example is for the Windows platform, first download the stand-alone installer from http://code.google.com/p/wkhtmltopdf/downloads/ (libwkhtmltox-0.11.0_rc1.zip). I installed my version to “C:\wkhtmltopdf” to keep things simple and without spaces within the folder structure.

In this example, we are going to convert Google to a PDF using only 1 line. I have created a new project in “C:\xampp\htdocs\test” for this example.

<?php
//create the pdf directory if it does not already exist
$new_dir = "pdf";
 if(!file_exists($new_dir)) {
  mkdir($new_dir);
 }

//this one line will execute WKHTMLTOPDF and convert Google.com into a PDF
echo shell_exec("c:\wkhtmltopdf\wkhtmltopdf http://google.com c:\xampp\htdocs\test\pdf\google.pdf");
?>
 

This script checks to see if a directory called “pdf” exists, and if not, creates it. The following PHP shell_exec() line executes WKHTMLTOPDF, grabs the HTML contents of http://google.com and then converts it to PDF. Yes, it is really this simple.

This article has 10 comments

  1. Livin' In China Reply

    Hi thank you for this very easy implementation of wkhtmltopdf.
    I did exactly the same as you did except that i’m using wamp so i corrected the path in the shell: shell_exec(“c:\wkhtmltopdf\wkhtmltopdf http://google.com c:\wamp\www\test\pdf\google.pdf”);

    The pdf folder is created correctly but as for any tutorial i followed for wkhtmltopdf, it’s still doesn’t work!

    I downloaded libwkhtmltox-0.11.0_rc1.zip from google. I unzipped the folder in C: and renamed it “wkhtmltopdf”. In your shell the path is “c:\wkhtmltopdf\wkhtmltopdf” but if i follow this path in my case, when i open c:\wkhtmltopdf i have3 folders: example, include and lib so this path c:\wkhtmltopdf\wkhtmltopdf leads nowhere for me.

    I’ve been trying to use wkhtmltopdf, but it’s really complicated to make it work.

    Do you have any idea what i’m doing wrong?

    Oh and by the way, if want want to have a page on my website with a simple button that would convert my webpage into a pdf for visitors, should i use a version for linux instead? My server is linux.

    Thank you very much for your enlightment and this article that make it simple compared to others online!

    Look forward to hear from you,
    Best regards

    • Michael Reply

      the example and tutorial i gave is for windows servers. linux servers use different commands that i am not familiar with. sorry, but i cannot help you since you use linux.

  2. raj Reply

    I have followed the same as u mentioned but its not happening for me …when i run the same in command prompt its working but from php its not.i have checked the apache error log there it says “‘C:\wkhtmltopdf\wkhtmltopdf’ is not recognized as an internal or external command,operable program or batch file.”.

  3. Febin Joy Keralam India Reply

    I downloaded libwkhtmltox-0.11.0_rc1.zip from google. I unzipped the folder in C: and renamed it “wkhtmltopdf”. In your shell the path is “c:\wkhtmltopdf\wkhtmltopdf” but if i follow this path in my case, when i open c:\wkhtmltopdf i have3 folders: example, include and lib so this path c:\wkhtmltopdf\wkhtmltopdf leads nowhere for me.

    I’ve been trying to use wkhtmltopdf, but it’s really complicated to make it work.

  4. Febin Joy Keralam India Reply

    download the wkhtmltopdf-0.9.9-installer.exe (wkhtmltopdf-0.9.9 Windows Installer (i368)) from the list. install it to c:\ .will get “c:\wkhtmltopdf\wkhtmltopdf” . i tried but i didnt get the pdf.
    this is my code, that made a directory named “pdfds” thats only, nothing inside.

  5. Chandu Reply

    Hi thanks for your tutorial and i do same as ” Livin’ In China ” told in windows but im facing same problem plz can u suggests me what wrong im doing

  6. Nitin Prakash Reply

    “‘C:\wkhtmltopdf\wkhtmltopdf’ is not recognized as an internal or external command,operable program or batch file.”.

    In windows run this command from administrator account
    e.g

    C:\wkhtmltopdf\wkhtmltopdf http://www.google.com google.pdf

  7. LagunaWebDesign Reply

    “…
    function isEnabled($func) {
    return is_callable($func) && false === stripos(ini_get(‘disable_functions’), $func);
    }
    if (isEnabled(‘shell_exec’)) {
    echo ‘Shell is enabled….’;
    }
    echo shell_exec(“C:\wkhtmltopdf\bin\wkhtmltopdf http://google.com C:\wamp\www\apdf\google.pdf”);

    …”

Leave a Comment

Your email address will not be published. Required fields are marked *