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.

13 Comments to “Creating PDF Documents in PHP Using Tcpdf”

  1. bharath 10 September 2013 at 12:40 am #

    how to generate a pdf using tcpdf in Zend framework2?

  2. Prog Tlake 17 May 2012 at 1:05 am #

    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

  3. Hari 19 August 2011 at 7:44 pm #

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

  4. dan 2 December 2009 at 11:28 am #

    @ojiepermana TCPDF has an active forum at http://sourceforge.net/projects/tcpdf/forums/forum/435311 that may be of some help.

  5. ojiepermana 29 November 2009 at 6:53 pm #

    how make header only page one….?

  6. dan 17 August 2009 at 3:45 pm #

    For tcpdf questions like these, your best bet is to ask at the TCPDF forum. I found a question on the subject

  7. lub 17 August 2009 at 1:18 am #

    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..

  8. dan 22 May 2009 at 6:11 pm #

    @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.

  9. moose2004 22 May 2009 at 1:20 am #

    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

  10. Ali 18 May 2009 at 5:57 pm #

    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!

  11. dan 12 May 2009 at 5:44 pm #

    @Lawson I am not sure what you mean by an HTML header? TCPDF is for creating PDF documents.

  12. Lawson 12 May 2009 at 5:06 pm #

    Can I create a HTML header using TCPDF

  13. [...] onemoretake » Creating PDF Documents in PHP Using Tcpdf. [...]


Leave a Reply