How to create PDF helper with TCPDF18 Dec 2011 Add Comments Intermediate Level
Table Of Content
- Download TCPDF from Sourceforge.
- Unpack downloaded folder, and copy it to directory "app/vendors/tcpdf"
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:
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.
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.