How to create PDF helper with TCPDF
18 Dec 2011 Add Comments Intermediate LevelTable 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.



20 Comments
Hi, I searched a lot about how to generate PDF with TCPDF in CakePHP. I feel lucky to find your tutorial so recent.
However, I tried your tutorial and it does not work. I am using CakePHP 2.1. Would you mind sharing how to do it in CakePHP 2.1.
THanks a lot!
Hi.
This tutorial is written in CakePHP 1.3.
But it should be very easy to convert to CakePHP 2.x.
What is the issue you encountered?
Regards
XD
Thanks XuDing, I think that I just got the configuration wrongly. Now I am able to make it work:)
Glad to hear that.
Help us share the site so that others can benefit from our tutorials too.
:)
Regards
XD
hello, how can i do to pas a variable from de controller to the view, and print that variable?? do you have any example?? also i need to pring a like 4 or 5 row how can i do that?? thanks from argentina
Hi.
Passing variable from controller to view: $this->set('viewName','value);
I assume you have knowledge with CakePHP, check out its documentation here http://book.cakephp.org/1.3/en/view/955/Controllers, and you will soon fall in love with this wonderful framework.
Regards
XD
I use http://www.pdfservices.net, create pdf from files html, css, php, ajax, mysql, printable and auto-print is better than any class I’ve used.
Thanks for the resource.
Regards
XD
thanks for your help, i did it with your tutorial. Regards
index.ctp este funciona con cakephp 2.1 es un ajuste en la forma en que se usa.
$this->Pdf->core->addPage('', 'USLETTER');
$this->Pdf->core->setFont('helvetica', '', 12);
$this->Pdf->core->cell(30, 0, 'Hello World');
$this->Pdf->core->Output('example_001.pdf', 'D');
?>
Hola, tienen algun ejemplo para traer informacion de la base de datos en un reporte? con TCPDF y Cakephp 2.1
I do not read your language.
;(
Regards
XD
Hello, have they algun example to bring information of the database in a report? With TCPDF and Cakephp 2.1
hello everyone
i get an error "Warning (2): Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\cakephp_final\views\helpers\date_picker.php:45) [APP\vendors\tcpdf\tcpdf.php, line 8757]TCPDF ERROR: Some data has already been output to browser, can't send PDF file"
Whats the problem on this? How to fix it?
i used cakephp 1.3 version.
It seems like it is caused by helpers\date_picker.php.
Check out if it is outputting anything.
Regards
XD
Sorry, I do not read your language
Regards
XD
Thank you so much.
For CakePHP 2.2 view should be:
$this->Pdf->core->addPage('', 'USLETTER');
$this->Pdf->core->setFont('helvetica', '', 12);
$this->Pdf->core->cell(30, 0, 'Hello World');
$this->Pdf->core->Output('example_001.pdf', 'D');
?>
Cannot modify header information - headers already sent by (output started at /var/www/html/ems/cake/libs/debugger.php:686) [APP/vendors/tcpdf/tcpdf.php, line 8776]
TCPDF ERROR: Some data has already been output to browser, can't send PDF file
where is my problem. Local Xampp server working but live server does not work.Please help me.
Hi . I need to print in PDF a list of name that I have in the "view/user/indext.ctp" . How can I make a pdf of that list ? I tried this tutorial but I don't understand the way to "print" a view. I could only print some HTML code with this tutorial. Who know how to help me ? thx.
If I wish to use this helper inside a controller function how would I do it? In this case I want to render multiple pdf's and store it in a file directory on the server, so basically I don't want to send it to the view but I want to return it to the function that stores the pdf file.