I needed to be able to generate PDFs for a client project and came upon this tutorial for using CakePHP and TCPDF. It turned out I needed to make some tweaks and after gathering info from different Google sources finally got it to work. Thought I’d save someone else the trouble and post my version.
Step 1:
Download the latest version of TCPDF.
Step 2:
Upload TCPDF files to app/vendor
Step 3:
Create your layout app/views/layouts/pdf.ctp
header("Content-type: application/pdf"); echo $content_for_layout;
Step 4:
Controller code (very basic)
function view_pdf($id = null) { if (!$id) { $this->Session->setFlash('Sorry, there was no PDF selected.'); $this->redirect(array('action'=>'index'), null, true); } $this->layout = 'pdf'; //this will use the pdf.ctp layout $this->render(); }
Step 5:
View code (page view_pdf.ctp)
See example code on the TCPDF site for details but here is a basic shell to get you going (I used writeHTML):
App::import('Vendor','tcpdf/tcpdf'); $tcpdf = new TCPDF(); $textfont = 'helvetica'; $tcpdf->SetAuthor("Julia Holland"); $tcpdf->SetAutoPageBreak(true); $tcpdf->setPrintHeader(false); $tcpdf->setPrintFooter(false); $tcpdf->SetTextColor(0, 0, 0); $tcpdf->SetFont($textfont,'',9); $tcpdf->AddPage(); // create some HTML content $htmlcontent = <<<EOF Add HTML content here to print EOF; // output the HTML content $tcpdf->writeHTML($htmlcontent, true, 0, true, 0); $tcpdf->Output('filename.pdf', 'D');
That’s it!

how can we convert pdf of content of view?
Hi,
This is good artical. I like it. I have worked on the TCPDF with cakePHP. First I have implemented TCPDF as given in cake “Bakery” But that give me some header error. Now I am using this artical and implemented in the same way as in this articl. Now my pdf genarater is working.
Thank
@umit Sorry, but I don’t understand what your question is.
@Sanjeev Glad it was helpful!
$tcpdf->Output(‘filename’.pdf’, ‘D’);
–
$tcpdf->Output(‘filename.pdf’, ‘D’);
Dear sir,
I am unable to generate pdf file using cake php i am trying from last 1 week but i failed more than 100 time lastly i tried your article but no use please help me you have any posibilty.
Thanks for your article I tried your article twice I got Good output . Thank you very much.
Thank you.
Nice and simple article.
Hi
I’m trying your tutorial with an html code that contains images.
I was wondering how do you write the path of the image in your html, because when you do it with the cakePhp helper, Tcpdf shows an error (it doesn’t find the image)….
Thanks a lot !
Thousand of kisses for you…..!!!
I have gone with the article in Bakery, but never seen ( Hello world: text ),
Now i just followed your instruction, and i am on a super-fast bullet train…..
……Many-many-many thanks for this article…..
I am going to give a link in bakey of this topic…..
Thanks again….U Simply Rocks!!!
Gret tutorial! I have it working but how do I add my content? I need the variables from the controller to be passed into the output pdf.
$this->set(‘user’, $user);
$this->layout = ‘pdf’;
$this->render();
How can I do that? tried a few things but nothing works.
Thanks again,
Dave
Hi Julia,
I have an issue: I unable to show a checked box, e.g. when i write:
I have a car
The PDF generated have a checkbox, but its not cheked….???
Can anyone help me???
Thanks in advance…!!!
@Dave Just as you’re setting the $user variable with the Controller::set() method, you should have it in your view (in view_pdf.ctp). There, you should do with it whatever you want, for example, add its contents to the $htmlcontent variable and it should be in the output.
Nice Post. Julia!
Does it also output arrays.. I tried to output an query from the controller. It does not work.
I appreciate any help.
Thanks again for the great tutorial.