Home > Web Development > WKHTMLTOPDF with PHP on Ubuntu linux

WKHTMLTOPDF with PHP on Ubuntu linux

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 :P )
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 )
<?php
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    $cmd = "/usr/local/bin/wkhtmltopdf http://www.yahoo.com /tmp/yahoo.pdf";
    $t = shell_exec($cmd);
    exit();

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";
About these ads
Categories: Web Development
  1. August 29, 2011 at 2:50 PM | #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,
    Rajkumar

    • August 29, 2011 at 4:10 PM | #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 18, 2012 at 6:49 AM | #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 18, 2012 at 5:24 PM | #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
      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

  3. Rajesh
    February 18, 2012 at 10:46 AM | #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 3, 2012 at 3:00 AM | #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 4, 2012 at 12:40 PM | #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 29, 2012 at 2:41 PM | #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 29, 2012 at 10:37 PM | #10

      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 !

  6. aprog
    November 1, 2012 at 12:50 PM | #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 1, 2012 at 1:10 PM | #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 14, 2013 at 8:22 AM | #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 14, 2013 at 12:27 PM | #14

      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 ?

      • naresh kumar
        June 14, 2013 at 1:05 PM | #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();

        $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:\wamp\www\naresh\testing\urltopdf1.php on line 82
        ( ! ) Exception: WKPDF couldn’t determine CPU (“”). in D:\wamp\www\naresh\testing\urltopdf1.php on line 82

      • June 14, 2013 at 1:17 PM | #16

        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

      • naresh kumar
        June 14, 2013 at 1:33 PM | #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:\wamp\www\naresh\testing>wkhtmltopdf http://www.google.com google.pdf
        ‘wkhtmltopdf’ is not recognized as an internal or external command,
        operable program or batch file.

      • June 14, 2013 at 1:42 PM | #18

        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

  8. naresh kumar
    July 26, 2013 at 2:15 PM | #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.

    <?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

    • July 26, 2013 at 2:50 PM | #20

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

      • naresh kumar
        July 31, 2013 at 12:27 PM | #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 31, 2013 at 12:54 PM | #22

        can you give me one example where

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

  1. February 23, 2012 at 11:10 PM | #1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: