Web DevelopmentHTMl 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


  1. October 21, 2015 1:40 pm

    thank you Mr. Naveeen
  2. October 21, 2015 5:38 am

    ok thank you.. but give me some syntax to show header and footer from external files including to both header and footer
  3. October 16, 2015 10:12 am

    how to set header and footer, page no and logo for pdf ?
    1. October 16, 2015 3:24 pm

      @kirangowda 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
    2. October 19, 2015 3:52 pm

      @kirangowda 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
      1. October 19, 2015 3:54 pm

        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
  4. Brandon

    July 26, 2015 10:18 pm

    Naveen, 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.
  5. naresh kumar

    July 26, 2013 2:15 pm

    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. <?php error_reporting(E_ALL); ini_set('display_errors', '1'); $url = $_POST['sendUrl']; $cmd = "/usr/local/bin/wkhtmltopdf ".$url." /tmp/google1.pdf"; $t = shell_exec($cmd); var_dump($t); exit(); http://stackoverflow.com/questions/17882584/how-to-get-linux-command-execution-result-or-message Thankx
    1. July 26, 2013 2:50 pm

      Naresh, 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 );
      1. naresh kumar

        July 31, 2013 12:27 pm

        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..
        1. July 31, 2013 12:54 pm

          can you give me one example where "Many other type of errors and issue can occur. and this approach is not good."
  6. naresh kumar

    June 14, 2013 8:22 am

    @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. :(
    1. June 14, 2013 12:27 pm

      Naresh 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 ?
      1. naresh kumar

        June 14, 2013 1:05 pm

        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(); $pdf->set_html($html); $pdf->render(); $pdf->output(WKPDF::$PDF_EMBEDDED,'sample.pdf'); 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
        1. June 14, 2013 1:17 pm

          Naresh, 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
      2. naresh kumar

        June 14, 2013 1:33 pm

        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.
        1. June 14, 2013 1:42 pm

          here is the tutorial for you - hope it helps on windows http://nenotlep.blogspot.fi/2012/12/php-wkhtmltopdf-and-win-7-eleven-steps.html
  7. aprog

    November 1, 2012 12:50 pm

    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...
    1. November 1, 2012 1:10 pm

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

    May 29, 2012 2:41 pm

    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!
    1. May 29, 2012 10:37 pm

      Sean, 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 !
  9. AJ

    May 3, 2012 3:00 am

    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?
    1. May 4, 2012 12:40 pm

      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
  10. February 23, 2012 11:10 pm

    [...] "jqmodal", embeddedHeight: "400", embeddedWidth: "425", themeCSS: "" }); . WKHTMLTOPDF with PHP « Naveen S Nayak’s Blog . HTMLtoPDF Converter: GUI de WkHTMLtoPDF [...]
  11. Rajesh

    February 18, 2012 10:46 am

    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.
    1. February 18, 2012 5:25 pm

      Please see the reply above
  12. vullivijaya

    February 18, 2012 6:49 am

    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.
    1. February 18, 2012 5:24 pm

      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 http://code.google.com/p/wkhtmltopdf/wiki/compilation Once you got the MinGW and other package mentioned there, you just need to follow check this page on how to install http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltoimage_0.10.0_rc2-doc.html 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
  13. August 29, 2011 2:50 pm

    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, Rajkumar
    1. August 29, 2011 4:10 pm

      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?

Post a comment

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