html to pdf PHP Ubuntu

HTML to PDF using wkhtmltopdf

HTML to PDF using wkhtmltopdf
Written by Yogesh Koli

HTML to PDF with wkhtmltopdf

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 it sounds good, only two steps right?

Step 1: Installing wkhtmltopdf on Linux (Ubuntu) Server:

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 http://wkhtmltopdf.org/downloads.html 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

Test by typing following command.

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 google.com 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.

  • Juan Carlos Campbell

    Hi again I use

    tar -xvf instead of unzip and it works

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