Sunday, March 9, 2008

HTML to PDF

The Dompdf application written in PHP is used to convert from HTML to PDF. Even though there are some constraints to it, but a normal html with CSS can be converted easily to PDF.

http://www.digitaljunkies.ca/dompdf/

The above link has provided detail information about how to use the application. We have implemented this in one of our project where we had converted a html which consists of header, table, logos and images to PDF in 3 different formats (Landscape, Horizontal and Portrait).

The advantage of the above application is either you can execute this on browser or on shell. We have executed on shell since our application is written in RoR.

Steps to convert to PDF

1. Have the html file ready which you want to convert to PDF.

Note: The PDF generated will be similar to the HTML. So make sure to have everything in HTML which you want to show in PDF.

2. Execute this command to convert to PDF

/usr/local/php/bin/php -f “/dompdf.php” -o "landscape" -p "landscape" -b ""

Example:

/usr/local/php/bin/php -f /export/home/mortal/devel/eco-audit/dompdf-0.5.1/dompdf.php 52_horiz.html -o "landscape" -p "landscape" -b "/export/home/mortal/devel/eco-audit/public/papers/"

The –p option is used to tell the dompdf the size of the pdf it has to generate. By default there will be some sizes available in this file /include/cpdf_adapter.cls.php. If you want your own custom size, you can add/edit the existing in the above php file. In our above example, we have included 3 sizes to the existing array in the php file.

The –o option is used to mention the orientation of the file. It is used to specify either “landscape” or “portrait”

The –b option is used to specify the directory of the input html file. If you are already in the existing directory, then you can skip –b option.

The –f option “dompdf.php” is the file to which all the above options will be passed as arguments.

3. The above 2 points will be enough to generate a PDF file based on the given html file. If you want the new fonts to be included, then you have to do the following changes.

· In , you will have all the fonts that will be used while generating pdf. If you want any new fonts to be used, specify the font in your html file and copy that font into your directory.

· Once you copy the fonts, check the permissions on the fonts.

· Then open dompdf_ font_family_cache file in lib/fonts directory. You will see an array with the fonts in this fashion

'sans-serif' =>

array (

'normal' => DOMPDF_FONT_DIR . 'Helvetica',

'bold' => DOMPDF_FONT_DIR . 'Helvetica-Bold',

'italic' => DOMPDF_FONT_DIR . 'Helvetica-BoldOblique',

'bold_italic' => DOMPDF_FONT_DIR . 'Helvetica-BoldOblique',

),

'times' =>

array (

'normal' => DOMPDF_FONT_DIR . 'Times-Roman',

'bold' => DOMPDF_FONT_DIR . 'Times-Bold',

'italic' => DOMPDF_FONT_DIR . 'Times-BoldItalic',

'bold_italic' => DOMPDF_FONT_DIR . 'Times-BoldItalic',

),
Now to use your font which you had already copied to lib/fonts directory, mention the font name in the above mentioned file in the same way as it is mentioned for other fonts. You have to understand that for every type of font “normal”, “bold”, “italic”, “bold-italic”, you will have a separate file in lib/fonts directory and you are pointing that file in the array and this array will use the pointed file while conversion

4. Apart from this, there is a file "dompdf_config_inc.php” which has all default configuration of the dompdf. You can specify the default paper size, default font, default DPI, etc.

No comments: