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.

Table Of Content

  1. Prepration
  2. PDF Helper class
  3. Usage
  4. The end

Content


1.Prepration
  1. Download TCPDF from Sourceforge.
  2. 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:

  1. Import the TCPDF class from vendor folder.
  2. 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.

  1. Create a layout file "app/views/layouts/pdf.ctp":
  2. Create a PdfsController class, and include PDF Helper. We will also create a index function for generating pdf:
  3. Now let us create a view file "app/views/pdfs/index.ctp", this file will utilize our PDF Helper class to generate a PDF:
  4. 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.