html to pdf PHP Ubuntu

Advance way to export HTML to PDF – wkhtmltopdf

HTML to PDF using wkhtmltopdf
Written by Yogesh Koli


Previously, I have posted tutorial on html to pdf conversion using DOMPDF, you can checkout here Convert HTML to PDF Using DOMPDF in PHPdompdf is quite good but it has few limitations it does’t work with big amount of data. I have been searching for a while to get better solution for my application, I need to generate more than 50 pages with a single request and it has a lot of complicated html table structure and so on.

When I tested dompdf with same request data, server got freeze because of long execution time and I had to restart my server again and again which is not good thing basically it is not working at all for big amount of data.

So here I come up with wkhtmltopdf library which is working really good for me. I tested it works like a magic,  I have generated 70 PDF pages within 6 seconds of execution time.

wkhtmltopdf  is not a PHP library it is command line tool which is written in C, it has really good options to convert html to pdf file, so let’s not spend time discussing it.

 I am going to provide a quick guide to setup wkhtmltopdf  on Linux server using Ubuntu operating system.

Steps we needs to follow while working on wkhtmltopdf library:

  1. Generate HTML file with the data you want to convert in the pdf file, you can add required style sheets (css) we can include external style sheet as well, just think of it like a web page, after generating the page store on the server with .html
  2. Use wkhtmltopdf command to generate pdf file.

Does this sounds good, only two steps right?

Step 1: wkhtmltopdf Installation:

Ubuntu 14.04

I have tested it with my cloud server which has Ubuntu 14.4 installed, if you don’t have server don’t worry you can test it with your local ubuntu operating system.

Use this link to download required version, in my case it is Linux 64 bit.

Download the library using wget, you can also download manually if you are on local server it all up to you.

Unzip download file:

Move wkhtmltox folder to bin directory to make it executable

Ubuntu 16.04

Download installer:

Install wkhtmltopdf:

Ubuntu 18.04

Download installer:

Install wkhtmltopdf:

You can verify installation using following command on Ubuntu 16.04 or Ubuntu 18.04

You should see – /usr/local/bin/wkhtmltopdf

Test PDF Conversion:

Test installation using following command.

Ubuntu 14.04:

Ubuntu 16.04 & Ubuntu 18.04:

if you see above command you will find there are three sections, the wkhtmltopdf executable is the first section and the url of the html file that needs to be converted and url of the pdf file where file is going to save after converting.

if test works for you it should show following output on the command line:

You can checkout page should get converted to the pdf file.

so that’s all we needs to do in the setup, you might think that this is all we are doing it manually How I can add it to the php project?

don’t worry I will guid to the next level to be able to use this library in the php project.

Step 2: Generate html to pdf using PHP script:

let’s create sample.php file and add following code.

Here we are adding a sample html data to the test.html file and then passing the same file to the command, try to follow the same steps at your end and let me know if you get it working correctly.

Feel free to comment if you get any issue while following this post.

About the author

Yogesh Koli

Yogesh Koli is a software engineer & a Blogger lives in India. He's driven by an addiction to learning and a love for adventure. he has 5+ years of experience working with the front-end, back-end, web application development, and system design.

3 Comment threads
2 Thread replies
Most reacted comment
Hottest comment thread
3 Comment authors
Yogesh KoliBalaYogesh KoliJuan Carlos Campbell Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

newest oldest most voted
Notify of

can i download the pdf in my local browser using this command

Yogesh Koli

if you have .zip file then you should use: unzip and if you see .tar file then you can use tar -xvzf filename.tar.xz

Juan Carlos Campbell
Juan Carlos Campbell

Hi again I use

tar -xvf instead of unzip and it works

Yogesh Koli