Convert HTML and CSS page to PDF and send in email attachement in PHP

I am mainting one web based product in php. Application is about hotel management for tariff calculation and all. My client has requested to email invoice as pdf generated by php based software.

I have divided process in two steps.

  1. Dynamically Generate PDF file some where on the server using html css produces by php
  2. Attach  this generated pdf invoice in email

First of all I did tried using dompdf famous php library to generate HTML css invoice but due to incompatibility with tables i did not get accurate invoice pdf as i want. So i started searching on google and also posted on google+. Finally from my google+ php community post i came across wkhtmltopdf which is really nice tool and solve my problem.

Wkhtmltopdf

Wkhtmltopdf is a command line utility that has nothing to do with php. But we can call this utility to generate pdf file from html.

Todo so first of all we have to download wkhtmltox from it’s website suitable for our OS. And than have to install it. In windows we will have exe where as in linux we have tar.gz.zip file which we have to extract. Once we have setup ready of wkhtmltox we can convert HTML+Css Page to pdf from command line

“C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe” D:/wamp/wamp64/www/php-playground/html-pdf/dom-pdf-example/dom-pdf-bill.html D:/wamp/wamp64/www/php-playground/html-pdf/dom-pdf-example/dom-pdf-bill.pdf

You can see in above command line first is wkhtmltopdf binary file path, second argument is html+css file which we want to convert in PDF and third argument is output file. Please note that here we have used full path. I am using windows so i have take an example of windows but on linux you can setup wkhtmltox inside your home folder by just copying the folder and give proper permissions.

But we need this utility to access from php . Below code will help us to achieve our goal.

In above example we have used PHP function shell_exec to execute binary file. By running above file from browser we have PDF file in defined location if error will not occur.

shell_exec is a good function to run binary file from PHP, but php have package wkhtmltopdf which is good and have provide good object oriented interface.To install phpwkhtmltopdf we have to run below command from command prompt

composer require mikehaertl/phpwkhtmltopdf

Once we installed phpwkhtmltopdf successfully we have to use this package. Below code will help us to understand usage of phpwkhtmltopdf

You can see we have pass few options in PDF class. The important line in above code line no 26 which sets the wkhtmltopdf.exe path from our installation directory. Set your own installation path for wkhtmltopdf.

But above example has fixed static html file and we need this convert dynamic PHP file which generates invoice for each customer. So how to achive this let see an example.

We have used PHP ob_start and ob_get_content to get HTML and CSS generated by PHP. ob_start function process php but does not print response in browser. To send an email we have used PHPMailer package. Properly configure PHPMailer paramters will help you send email with attachment.

In conclusion wkhtmltopdf has done nice job for building this utility. It creates nice and perfact PDF file for HTML based contents.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

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