How to create PDF helper with TCPDF
18 Dec 2011 Add Comments Intermediate Level
In this tutorial, we will show you how to create a PDF helper using TCPDF library.
Our focus will be how to create the helper class, if you want to find out more about TCPDF library as well as its usage.
Please visit its official site at http://www.tcpdf.org.
1.Prepration
2.PDF Helper class
3.Usage
4.The end
Table Of Content
Content
1.Prepration
- Download TCPDF from Sourceforge.
- Unpack downloaded folder, and copy it to directory "app/vendors/tcpdf"
2.PDF Helper class
Create "pdf.php" and place it to directory "app/views/helpers/pdf.php". Below is the code for the class:
- Import the TCPDF class from vendor folder.
- Extend PdfHelper from AppHelper class.
From the code above, we can tell PdfHelper class is actually just a wrapper class for TCPDF. Whenever you want to use TCPDF's functions, you simply do:
3.Usage
Let us test out this helper class.
- Create a layout file "app/views/layouts/pdf.ctp":
- Create a PdfsController class, and include PDF Helper. We will also create a index function for generating pdf:
- Now let us create a view file "app/views/pdfs/index.ctp", this file will utilize our PDF Helper class to generate a PDF:
- Now go to your browser and type in address to access /pdfs/index. You should be prompted to download a PDF file.
4.The end
Hopefully this simple tutorial helped you with your development.
If you like our post, please follow us on Twitter and help spread the word. We need your support to continue.
If you have questions or find our mistakes in above tutorial, do leave a comment below to let us know.



2 Comments
thank you for this nice tutorial! But I'm getting an error using this stuff in CakePHP 2.
Notice (8): Undefined variable: pdf [APP\View\Pdfs\index.ctp, line 2]
Notice (8): Trying to get property of non-object [APP\View\Pdfs\index.ctp, line 2]
Fatal error: Call to a member function addPage() on a non-object in \app\View\Pdfs\index.ctp on line 2
Do you know what could be the problem?
Regards
Hi Hannes
Thanks.
Looks like you are using CakePHP 2.0, I have not tested this on CakePHP 2.0 yet. This tutorial is written in CakePHP 1.3
Try to use $this->Pdf inside the view file instead of $pdf.
Let us know if it works.
Cheers.