Previously, I have posted tutorial on html to pdf conversion using DOMPDF, you can check out here Convert HTML to PDF Using DOMPDF in PHP. dompdf is quite good but it has few limitations it doesn’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:
- 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
- Use wkhtmltopdf command to generate pdf file.
Does this sound good, only two steps right?
Table of Contents
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 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.
$ wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
Unzip download file:
$ tar xf wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
Move `wkhtmltox` folder to bin directory to make it executable
$ mv wkhtmltox/ /usr/local/bin/
Ubuntu 16.04
Download installer:
$ wget https://downloads.wkhtmltopdf.org/0.12/0.12.5/wkhtmltox_0.12.5-1.xenial_amd64.deb
Install wkhtmltopdf:
$ sudo dpkg -i /absolute/path/to/wkhtmltox_0.12.5-1.xenial_amd64.deb
$ sudo apt-get install -f
Ubuntu 18.04
Download installer:
$ wget https://downloads.wkhtmltopdf.org/0.12/0.12.5/wkhtmltox_0.12.5-1.bionic_amd64.deb
Install wkhtmltopdf:
$ sudo dpkg -i /absolute/path/to/wkhtmltox_0.12.5-1.bionic_amd64.deb
$ sudo apt-get install -f
You can verify installation using following command on Ubuntu 16.04 or Ubuntu 18.04
$ which wkhtmltopdf
You should see – /usr/local/bin/wkhtmltopdf
Test PDF Conversion:
Test installation using following command.
Ubuntu 14.04:
$ /usr/local/bin/wkhtmltox/bin/wkhtmltopdf http://www.google.com google.pdf
Ubuntu 16.04 & Ubuntu 18.04:
$ /usr/local/bin/wkhtmltopdf http://www.google.com google.pdf
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:
yogesh@ubuntu:~$ /usr/local/bin/wkhtmltopdf http://www.google.com google.pdf
Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done
You can checkout google.com page should get converted to the pdf file.
so that’s all we need 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 guide 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.
<?php
$data = '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h1>Lorem Ipsum</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quod laboriosam atque, consequuntur beatae fugiat in incidunt perspiciatis dolorem ipsum nam itaque animi nobis quam illum dolore. Nam nostrum maiores, ipsa.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsa vero veniam sed ullam commodi! Accusantium, officia vitae, adipisci mollitia, blanditiis voluptates accusamus nam atque repellendus laborum, ullam vel eligendi esse?</p>
</body>
</html>';
file_put_contents('/var/www/domainname.com/html/test.html', $data);
$html_file_url = '/var/www/domainname.com/html/test.html'; // html file
$pdf_file_url = '/var/www/domainname.com/pdf/test.pdf'; // pdf file
$cmd = "/usr/local/bin/wkhtmltopdf $html_file_url $pdf_file_url";// command
exec($cmd); // execute command from php
?>
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.
Hi again I use
tar -xvf instead of unzip and it works
Great..!!
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`
can i download the pdf in my local browser using this command
Yes you can download, you just need to access and open converted file from the folder and add required headers. You can take the reference from this tutorial – How to create dynamic zip Archive file in PHP?
How to use in Windows 10 with PHP script.