I will explain in this article how to make a pdf generator from html in php.
We will use a command tool that will generate the pdf. It’s an open source shell utility that is called wkhtmltopdf that uses webkit rendering engine, and qt – go here for more information for wkhtmltopdf. I suggest to use the version 0.9.9, which I think it’s a stable version right now. I tested the version 0.11.0_rc1 and I got some problems with downloading the images from html img tag.
Steps to generate the pdf from html:
1. Download the tool.
2. Install the tool.
3. Use the tool from your php code to create a pdf from html.
1. Depending on your operating system you should download from the following page:
2. To install on Windows should download the .exe from the website, launch the setup and follow the steps. In order to test if you installed correctly the tool you should go to the folder of where you installed it and launch in command prompt the following command: wkhtmltopdf http://www.test.com test.pdf
To install on Linux you should find out the operating system launching the command uname -a that will tell you the operating system you are using and download the apropiate version of the tool.
To install on 32 bits Linux operating system you should use the following code:
//get the file from server
wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-i386.tar.bz2
tar xvjf wkhtmltopdf-0.9.9-static-i386.tar.bz2
// move the application to bin folder
mv wkhtmltopdf-i386 /usr/local/bin/wkhtmltopdf
//change permission for install it
chmod +x /usr/local/bin/wkhtmltopdf
// install it
/usr/local/bin/wkhtmltopdf
//To test if you installed correctly the tool
wkhtmltopdf-i386 http://www.test.com test.pdf
To install on 64 bits Linux operating system you should use the following code:
//get the file from server
wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-amd64.tar.bz2
tar xvjf wkhtmltopdf-0.9.9-static-amd64.tar.bz2
// move the application to bin folder
mv wkhtmltopdf-amd64/usr/local/bin/wkhtmltopdf
//change permission for install it
chmod +x /usr/local/bin/wkhtmltopdf
// install it
/usr/local/bin/wkhtmltopdf
//To test if you installed correctly the tool
wkhtmltopdf-amd64 http://www.test.com test.pdf
3. In php you should execute the tool using shell_exec:
shell_exec("/usr/local/bin/wkhtmltopdf-i386 http://test.com test.pdf");
//for Linux 32 bits operating system
//for Linux 64 bits operating system use wkhtmltopdf-amd64
//for windows just put the path of the exe file.
$allfile = file_get_contents("test.pdf");
header('Content-Type: application/pdf');
header('Content-Length: '.strlen($allfile));
header('Content-Disposition: inline; filename="test.pdf"');
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
ini_set('zlib.output_compression','0');

















