Creating PDF Documents in PHP Using Tcpdf

When developing websites, it is not going to be too long before you are required to generate a pdf for users to download. One usually searches for a suitable library for this and in php, there are a few options out there. My personal favourite, and one that is still receiving regular updates and improvements, is tcpdf.

There always seems to be a trade-off between flexibility and ease of use with these libraries and tcpdf seems to lean more towards the flexibility side of things. I prefer this, as there would be nothing worse that committing oneself to a particular library, only to find that it is not possible to generate a particulr report further down the track. On top of this, the library is available in PHP4 and PHP5 versions and is open source, should one need to tweak it any way.

Installation is pretty straight forward for and there are suitable instructions telling you how. In my case, however, I am using the CodeIgniter framework and needed to take some additional steps in order to use it. If anyone else is also using CodeIgniter, here is how I do it:

  1. Unpack the tcpdf installation package into your  system/plugins folder. This will give you a tcpdf directory in the plugins directory.
  2. Create a tcpdf_pi.php file inside the plugins directory. Place the following code in it:
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
 
// Extend the TCPDF class to create custom Header and Footer
class OnemoretakePDF extends TCPDF {
}
 
function tcpdf(){
    return new OnemoretakePDF (PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true);
}
 
?>

Here, we basically instantiate tcpdf and pass in a few of the default values. You may be wondering why I have extended the TCPDF class and have not just instantiated it directly. Well, I shall come to that later.

To use this in CodeIgniter, you load the plugin and then create an instance of  it as follows:

1
2
$this->load->plugin( 'tcpdf' );
$pdf = tcpdf();

The next step really, is to visit the documentation for tcpdf. It is not the easiest to follow set of documentation there is, as it has been generated directly from the code, although there are several examples that you can pull apart to learn more. The premise here is to create a new pdf page and plot what you want on that page. This can be done directly with commands like Text() and Line() which use coordinates directly or you can also use Cell() and MultiCell() which allow you to draw using a cell system like a table and utilise borders and alignment appropriately. For those who just cannot get their heads around plotting on a page, there are commands to plot HTML, although support for styles is limited, so don’t expect to recreate a fancy webpage layout by throwing the html at it. I found that with a little bit of lateral thinking, most jobs can be done without the HTML methods anyhow.

What took me a little time to get used to, is that the coordinate system is not in pixels but by default, in millimetres. This actually makes a lot of sense, as we are creating for print here, not screen. This makes it very awkward if you are working off a design done in photoshop or similar and want to get the dimensions just right. However, a trick I soon learned was to print that image directly to a pdf and then you can use the measurement tools in your pdf reader (Foxit reader has such a tool) to measure the correct distance in millimetres.

The API for tcpdf is extensive and with it, one can achieve most tasks. One thing to note, however is that to create a standard header and footer for your document, you need to override the default header and footer methods in the main tcpdf class. This is why, in the code above, I extended the tcpdf class. It will give us the opportunity to override these methods as follows:

1
2
3
4
5
6
7
8
9
10
11
class OnemoretakePDF extends TCPDF {
    //Page header
    public function Header() {
        //header plotting code here
    }
 
    // Page footer
    public function Footer() {
      //footer plotting code here
    }
}

My biggest gripe about this library is that the documentation does not give enough detail. Coupled with that, the forum is a SourceForge forum, which frankly I find very difficult to find anything in. It would be great if there was a way to get more detail, perhaps a wiki would benefit the project a great deal – I am sure there is a lot of detailed knowledge out there about the ins and outs of the library, its just that there is no way to tap into it.

You can’t complain too much however, this project provides a very powerful pdf creation library. I have had experience with Microsoft.Net libraries of a similar nature and you can end up paying quite a lot for them. Not to mention they tend be way more restricted as to what is possible.

 

17 thoughts on “Creating PDF Documents in PHP Using Tcpdf

  1. Hii
    Can you please tell,
    That can I use any website URL or Webpage as in TCPDF to get its PDF file….
    I have a quotation making system, and i want it’s PDF, but my file having more PHP code so, it have been very difficult to me to take its PDF.
    So can I get PDF from URL or iframe or by webpage…. if yes, so how ?

  2. Currently, we are working on HTML to pdf convert in PHP for printing material. According to printing standard, We can not use png in Html.

    A Below listed plugin is not already tried to use .tiff images but most of the plugin convert tiff into jpg or not supported.

    tcpdf
    fpdf
    Html2pdf
    Dom pdf

    Can you please suggest me if any plugin provides .tiff support in html 2 pdf?

  3. I have installed TCPDF library to my fedora machine.
    But I am not able to use it to my Controller.
    please tell me should i copy it to my zend’s library and use it by giving it namespace.???
    Please Help Me

  4. Pls i need a clear tutorial on how generate pdf report using php
    And after downloading the library, what steps must be taken to create pdf document
    Thanks i will be very happy if my request is treated

  5. how can i generate pdf, the content which is in my iframe graph, any Idea. pla help me …

  6. I also download TCPDF and go through the examples but the example number 14 is not working it.means it not displaying text box ,button, combo box. please tell me why it is not working on my site.
    I have Adobe Reader 8.

    Thank you..

  7. @moose2004 – Do you mean you want to create a pdf of a webpage at a particular url? I don’t think that you can do that directly. You could however get the html at the url using curl and then pass that html string into tcpdf using writeHTML. You have to be careful though as html support in tcpdf is limited. See the documentation on writeHTML for more details.

  8. I download TCPDF and went through all examples very nice!! But i have only one question, how do you pass only a url link to create a PDF?

    moos2004

  9. Most probably Lawson wants to use HTML header to be printed as PDF header. Answer is yes, but define embedded styles using table for layout – the old way.

    Good Article Dan!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.