Home > Web Development > HTMl to PDF using WKHTMLTOPDF on Ubuntu linux

HTMl to PDF using WKHTMLTOPDF on Ubuntu linux

Some things to understand before we begin

WKHTMLTOPDF is not a php utility

Its a command line C library to convert html files to pdf

I am only using PHP here to generate the HTML files i need to convert

You can use any language of your choice to generate the HTML files

So the steps would be

  • Generate the HTML of the page with all the required CSS in it
  • Save this page with an html extension somewhere in your file system
  • use the wkhtmltopdf command to convert the saved HTML file to PDF

If you are trying to do this on Windows – this might help : http://nenotlep.blogspot.fi/2012/12/php-wkhtmltopdf-and-win-7-eleven-steps.html

This is for using wkhtmltopdf command line utility in Ubuntu, Apache and PHP

  1. wkhtmltopdf is one of the best html to pdf conversion tools i have come across
  2. It is a command line utility
  3. It is very fast and converts most html pages – impressive  css support
  4. Integration with php is a breeze ( after struggling for 6 hrs 😛 )
Here is how to generate PDF files
  1. Download the latest wkhtmltopdf STATIC file from the site – at present it is wkhtmltopdf-0.10.0_rc2 Linux Static Binary (i368)  for Linux 32 bit
  2. There is also a wkhtmltoimage – dont download that accidentally and try to get pdf’s 🙂
  3. Please read the SYSTEM requirements carefully – wkhtmltopdf requires X11 libraries – which are not present on a typical bare-bone web server – you may need to install them
  4. unzip the file ( and rename it to wkhtmltopdf – optional)
  5. make sure that wkhtmltopdf is in your SYSTEM PATH – my PATH in UBUNTU is /usr/local/bin  – you may have a different PATH – find it out by using the command env  on the terminal
  6. I copied the wkhtmltopdf to this folder – so the file path now is /usr/local/bin/wkhtmltopdf
  7. From  the terminal issue this command /usr/local/bin/wkhtmltopdf http://www.google.com /tmp/google.pdf 
  8. Basically we are trying to convert Google’s home page into a pdf file ( change destination folder if you need )
  9. Command syntax is path-to-wkhtmltopdf path-to-html-file path-to-pdf-file
  10. Make sure that the destination directory has write access to apache
  11. If a pdf file is generated, then you have successfully installed wkhtmltopdf
  12. To make it work in a web environment, the apache user (www-data on Ubuntu) needs to have execute permissions to this file /usr/local/bin/wkhtmltopdf 
  13. After you have given the correct permissions, its time to create a small php script to test ( lets say at localhost/test.php )
    ini_set('display_errors', '1');
    $cmd = "/usr/local/bin/wkhtmltopdf http://www.yahoo.com /tmp/yahoo.pdf";
    $t = shell_exec($cmd);

Run this php script on a browser and check if the pdf file was created

If you have a local html file and want to convert it then just use

$cmd = "/usr/local/bin/wkhtmltopdf path-to-html-file path-to-pdf-file";
Categories: Web Development Tags:
  1. August 29th, 2011 at 14:50 | #1

    Hi Navenn,

    Thanks Thanks Thanks alot for this post.

    This post is really helped me…….:) ………. You are Great…………:)

    But background color are not displaying can you help me on this ?

    Thank You,

    • August 29th, 2011 at 16:10 | #2

      Hey Krishna,

      I tried converting to pdf with a simple html file and it worked –

      In your html head section

      body {
      background-color: green;
      color: white;

      In your html body section

      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

      PDF is generated correctly with the green background color

      Are you trying to do anything different?

  2. vullivijaya
    February 18th, 2012 at 06:49 | #3

    Hi Naveen,

    Can you please let me know whether we need to use X11 library to generate images from given website by using wkhtmltoimage.Can you please provide installation process for wkhtmltoimage in windows Operating system for testing process.I have placed wkhtmltoimage file in the C:Program Files in my system.

    • February 18th, 2012 at 17:24 | #4

      I have not tried using wkhtmltoimage on a windows or a linux machine – but for windows you will not need X11 libraries
      You would need something like CigWin or MinGW to be installed on to the windows system

      This link will tell you how to install the MinGW – just scroll till it says Windows

      Once you got the MinGW and other package mentioned there, you just need to follow check this page on how to install

      i read it briefly and they say the static binary should work on most systems – so after setting up MinGW – just copy the static binary and try to run the commands and see if it works

      hope this helps

  3. Rajesh
    February 18th, 2012 at 10:46 | #5

    Hi Naveen,

    Can you please let me know whether we need to use X11 library to generate images from given website by using wkhtmltoimage.Can you please provide installation process for wkhtmltoimage in windows Operating system for testing process.I have placed wkhtmltoimage file in the C:Program Files in my system.

  4. AJ
    May 3rd, 2012 at 03:00 | #7

    I am able to generate PDF from command line but I just get a blank white page when trying to do it through PHP. What could be wrong here?

    • May 4th, 2012 at 12:40 | #8

      There might be couple of things going wrong – some things to troubleshoot are
      1. Is the wkhtmltopdf installation proper ( which seems proper as the command line can generate pdf )
      2. Does apache ( or the web user ) has execute access to the wkhtmltopdf ?
      3. when calling from PHP – are you using the full path to wkhtmltopdf
      4. are file permissions proper ( read for the html file and write permission to the output dir ) correct for the web user ?
      5. the best option is to run the php script and immediately check the apache logs – it will display any errors

  5. Sean
    May 29th, 2012 at 14:41 | #9

    hey Naveen, I was wondering if you could run through the best way for apache to have access to just wkhtmltopdf. I need to make this air tight and would like to have as little of a security hole as possible. Right now it is not executing through apache. Before I go and mess with the .conf file I would like to know the best way to approach this. Thanks!

    • May 29th, 2012 at 22:37 | #10

      I think there is not much you can do for a tighter configuration. For a simple setup, make sure that the wkhtmltopdf has only read+execute for the apache user and keep a log for every pdf you generate. We had this kind of setup when we started – but as the need for pdf generation increased, we had to reconfigure
      This is what we currently do ( might be a bit too much for single server installs )
      1. We have a dedicated PDF generation server ( tightly locked – only the web-servers can talk to it )
      2. There is a common shared drive between the PDF server and other web servers – into which the HTMl file is put and the PDF file is put as well.
      3. A wrapper PDF generation class is used to generate all pdf’s from html files ( performs logging )
      4. Applications generate html files, put it on the common drive, call the PDF class with an identifier for the file
      5. PDF class reads the file from the shared drive, converts to pdf, puts the pdf in the shared drive, sends back the path to the pdf file to the application.
      6. the application can do whatever it wants with the file

      There are other ways using apache’s .htaccess – but i feel they will complicate things – you can read about htaccess files on the web

      hope this helps !

  6. aprog
    November 1st, 2012 at 12:50 | #11

    Is there a way to create a pdf file from an html link/page without using shell_exec() command as I am running code on my Linux shared web-server and I cannot configure my php.ini to turn off the safe mode… Please reply asap as I have tried many things but can’t figure it out…

    • November 1st, 2012 at 13:10 | #12

      it might be difficult to install wkhtmltopdf on a hosted shared server as it requires X11 libraries and i think most hosting services will not allow you to do it – check with your provider – if they allow you to do it, then great.

      You will have to use the command line to generate PDF reports ( or PHP command line functions )

      If none of this is possible, then you might have to look at TCPDF or FPDF libraries that can generate PDF files

      or a workaround is to host your own local web server( that can convert files using wkhtmltopdf ) and somehow make requests from the hosted server to your local server to convert files ( look the the response i wrote for Sean )

  7. naresh kumar
    June 14th, 2013 at 08:22 | #13

    @naveen ji : i wants to create a pdf from a url like https://www.google.co.in/ on localhost WAMP/XAMPP.
    i also tried with dompdf and also with this wkhtmltopdf. but failed in pdf result. can you give me tutorial or link from where i can solve my problem.
    I also tried with your tutorial but not solving problem. 🙁

    • June 14th, 2013 at 12:27 | #14


      Without knowing what the error or problem is, i cannot suggest anything – can you describe the problem ?
      You said you tried wkhtmltopdf – what happened – what was the error ?

      • naresh kumar
        June 14th, 2013 at 13:05 | #15

        sir first of all from scratch.
        i visited on this link http://code.google.com/p/wkhtmltopdf/wiki/IntegrationWithPhp and save his code in a file and used this code

        $html = file_get_contents(“http://www.google.com”);

        //echo $html;

        $pdf = new WKPDF();




        errors :
        ( ! ) Fatal error: Uncaught exception ‘Exception’ with message ‘WKPDF couldn’t determine CPU (“”).’ in D:wampwwwnareshtestingurltopdf1.php on line 82
        ( ! ) Exception: WKPDF couldn’t determine CPU (“”). in D:wampwwwnareshtestingurltopdf1.php on line 82

        • June 14th, 2013 at 13:17 | #16


          Did you try creating PDF as i have mentioned in my original post ? I do not say the way i am doing it is better, but from the link you mentioned, i dont understand why you need such a complex set of classes, finding type of CPU etc for a simple task of 3 to 4 line command line PDF generation.

          i would recommend to try my tutorial first – it involves minor things and 4 of 5 lines of code thats all – you can start generating PDF’s in less than 10 mins

          if you find problems from my tutorial, let me know and i will troubleshoot it for you

      • naresh kumar
        June 14th, 2013 at 13:33 | #17

        yes sir ji i have also tried with your guidance.
        you guided on linux OS and i am on Windows. on localhost.

        but i have tried your tutorial .. first of all from scratch i have download wkhtmltopdf-0.11.0_rc1-static-i386.tar. and from command line i have tried and then result is

        D:wampwwwnareshtesting>wkhtmltopdf http://www.google.com google.pdf
        ‘wkhtmltopdf’ is not recognized as an internal or external command,
        operable program or batch file.

  8. naresh kumar
    July 26th, 2013 at 14:15 | #19

    Sir ji according to your tutorial i have tried on Ubuntu
    Now problem is how to get linux command execution result or message

    I am using this code and want to convert html to pdf. I am on Linux.

    I am using Ajax on client side and PHP is server side script.

    no any error is coming. PHP code and Ajax code is working fine..

    with PHP code PDF is converting successfully. but after conversion PDF how could i get message is that PDF is converted or not converted or error are there.

    I also checked output of shell_exec() but “Null” is coming in both case when PDF is converted and not converted.

    Simply i need any Boolean value or some message which will address the state. How could i get that.

    ini_set('display_errors', '1');

    $url = $_POST['sendUrl'];
    $cmd = "/usr/local/bin/wkhtmltopdf ".$url." /tmp/google1.pdf";
    $t = shell_exec($cmd);




    • July 26th, 2013 at 14:50 | #20


      A simple thing would be to check if the PDF file exists after PDF generation command – just use php file_exists() and it will tell you if the PDF file was created or not – something like

      $t = shell_exec($cmd);
      $pdf_created = file_exists( pdf_file_path );

      • naresh kumar
        July 31st, 2013 at 12:27 | #21

        Sir i think this is not a good solution. its call in our desi language Jugad.. 🙂 m js joking. 🙂
        Many other type of errors and issue can occur. and this approach is not good..

        • July 31st, 2013 at 12:54 | #22

          can you give me one example where

          “Many other type of errors and issue can occur. and this approach is not good.”

  9. Brandon
    July 26th, 2015 at 22:18 | #23

    Are you available for a simple web development project? We need someone to generate a multi-page pdf from chart.js charts. All the charts are already coded just need to generate pdf.

  10. October 16th, 2015 at 10:12 | #24

    how to set header and footer, page no and logo for pdf

    • October 16th, 2015 at 15:24 | #25


      you have to do all in html first – the utility is just to convert html page to PDF
      so build your html with the page number, header footer and whatever you want and then just convert it

    • October 19th, 2015 at 15:52 | #26


      why dont you try this step by step ?

      1. first run your php script to generate the full html for the page you want to convert
      2. store this file along with css on your local file system somewhere with a .html extension
      3. now use whktmltopdf command to just convert the html file to pdf

      • October 19th, 2015 at 15:54 | #27

        wkhtmltopdf is NOT a php utility – its a library used to convert html files to pdf – you cannot pass PHP variables to it – i hope that makes is a bit clear

  11. October 21st, 2015 at 05:38 | #28

    ok thank you.. but give me some syntax to show header and footer from external files including to both header and footer

  12. October 21st, 2015 at 13:40 | #29

    thank you Mr. Naveeen

  1. February 23rd, 2012 at 23:10 | #1