SnapShooter Backups Server, Database, Application and Laravel Backups - Get fully protected with SnapShooter

How to create PDF helper with TCPDF

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


  1. Download TCPDF from Sourceforge.
  2. Unpack downloaded folder, and copy it to directory "app/vendors/tcpdf"

PDF Helper class

Create "pdf.php" and place it to directory "app/views/helpers/pdf.php". Below is the code for the class:

App::import('Vendor','TCPDF',array('file' => 'tcpdf/tcpdf.php'));   //1
class PdfHelper  extends AppHelper                                  //2
    var $core;
    function PdfHelper() {
        $this->core = new TCPDF();                                  //3
  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:



Let us test out this helper class.

  1. Create a layout file "app/views/layouts/pdf.ctp":


header("Content-type: application/pdf");
echo $content_for_layout;
  1. Create a PdfsController class, and include PDF Helper. We will also create a index function for generating pdf:
class PdfsController extends AppController {
     var  $uses = null;
     var  $helpers = array('Pdf');
     function index() {
  1. Now let us create a view file "app/views/pdfs/index.ctp", this file will utilize our PDF Helper class to generate a PDF:
$pdf->core->addPage('', 'USLETTER');
$pdf->core->setFont('helvetica', '', 12);
$pdf->core->cell(30, 0, 'Hello World');
$pdf->core->Output('example_001.pdf', 'D');
  1. Now go to your browser and type in address to access /pdfs/index. You should be prompted to download a PDF file.

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.