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 = <<;
 
// output the HTML content
$tcpdf->writeHTML($htmlcontent, true, 0, true, 0);
$tcpdf->Output('filename.pdf', 'D');

That’s it!