generating pdf files from html in php

Generating PDF Files From Html Using TCPDF

In this article i will illustrate how to generate pdf files from html in PHP using the open source TCPDF library.

 

 

There are a lot of open source libraries in php that can be used to generate pdf files from html, but most of them lack some pdf features such as how to manipulate text written in Right to Left languages, also handling the different font types.

 

TCPDF in the other hand is a powerful open source library that has almost all the features needed to generate a well working pdf files, from these features:

  • all standard page formats, custom page formats, custom margins and units of measure.
  • UTF-8 Unicode and Right-To-Left languages
  • TrueTypeUnicode, OpenTypeUnicode, TrueType, OpenType, Type1 and CID-0 fonts.
  • methods to publish some XHTML + CSS code, Javascript and Forms.
  • images (PNG, JPG, GIF), graphic (geometric figures) and transformation methods.
  • automatic page header and footer management.
  • document encryption up to 256 bit and digital signature certifications.

Check out TCPDF official website to figure the rest of the available features.

 

Download & Usage

Go to the github repository of TCPDF and clone or download it.

You need to add the library in your project folder and then include it like this:

By including the main tcpdf.php you have access to all classes and methods.

 

Configuration

The main configuration file located in TCPDF/config/tcpdf_config.php. This file contains the general configuration that applies as per document basis when generating pdf files. Try to uncomment and change the configuration values to suite your needs.

Adjust these configurations values:

The above configurations is the basic configurations needed but there more you can explore them by yourself as we don’t cover them in this tutorial.

As shown i have created a new folder for images in the project root folder, this will be used to store the header logo:



Example:

Let’s create a new file in the project root directory called index.php with the following contents:

index.php

Run this example you should see the pdf document opened in the browser itself. Now let’s explain the code below.

At first when generating pdf documents there are two steps. In the first step we setup the overall document appearance, this include setting fonts, colors, document title, header, footer, and the margins.

In the second step we begin by creating the pages by calling method AddPage(), then prepare the html to display, then call writeHtml() method and finally output the file.

 

In the above code we begin the first step by creating a new instance of TCPDF using the TCPDF() constructor like this:

The constructor accepts a list of arguments like the page orientation, page format in that case it’s “A4“, the encoding, here it’s “UTF-8” which supports RTL languages.

Then i set the metadata for the document like creator and author:

Then i set the header and footer data:

The setHeaderData() method accepts the header logo, the logo width, the header title, the header string text, all of these constants defined in the configuration file we described above. The other  two arguments represents the RGB color for the text and line.

After that i set the header and footer fonts as shown:

Those two methods accepts an array with the font family and font size.

It’s important also to set margins for the document, this achieved by calling those methods:

Then i enabled the option of auto page break by calling SetAutoPageBreak(). Also for the images i set the scale ratio to be 1.25 by calling setImageScale(). Finally i set the font for the document text by calling setFont() method which takes the font family, style, size, and the font file.



The second step i added a new page like this:

When ever you need to add a new page you must call this method. Next i specified the html text to be written to pdf file then we called writeHtml() method to write the html to the pdf page:

Finally to generate the file:

The Output() method takes the file name and a second parameter that defines a character that represent the destination to send the document to:

  • The “I” option means send the file to be displayed in the browser.
  •  “D” which send the file to the browser and force a file download.
  • “F” save to a local server file.
  • “S” return the document as a string.

 

Adding new pages:

Adding new pages is done easily by calling AddPage() method, let’s add two more pages to the above example, add this code after $pdf->writeHTML($html):

 

Adding images:

You can add images easily in html and TCPDF will handle the hard work to display those images, let’s add a new page which has an img tag:

 

Html & CSS:

This library comes with ability to apply css code to the html text then it will transform this to the corresponding pdf styling like in this example:



Right to Left languages:

As we mentioned earlier that TCPDF has a full support of RTL languages using the “UTF-8” encoding that we passed when we initialized the TCPDF() constructor, so let’s add a new page which has Arabic text.

As seen above i have added some arabic text and it should display them correctly. The library has a lot more more other functions for formatting and manipulating pdf unfortunately i will not cover all of these in this tutorial but i encourage you to explore them in the TCPDF official website.

Share this: