CakePHP and TCPDF

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! :)

13 Responses to “CakePHP and TCPDF”

  1. umit says:

    how can we convert pdf of content of view?

  2. Sanjeev Kumar says:

    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

  3. Julia says:

    @umit Sorry, but I don’t understand what your question is.

    @Sanjeev Glad it was helpful! :)

  4. johnny says:

    $tcpdf->Output(‘filename’.pdf’, ‘D’);

    $tcpdf->Output(‘filename.pdf’, ‘D’);

  5. Narasimham says:

    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.

  6. Narasimham says:

    Thanks for your article I tried your article twice I got Good output . Thank you very much.

    Thank you.

  7. balu says:

    Nice and simple article. :)

  8. phil says:

    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 !

  9. navdeep_aka_max says:

    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!!!

  10. Dave says:

    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

  11. navdeep_aka_max says:

    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…!!!

  12. Dave says:

    @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.

  13. Nick says:

    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.

Leave a Reply

Spam Protection by WP-SpamFree