Wednesday, February 15, 2012

Generate PDF files in CakePHP using TCPDF

What is TCPDF?
TCPDF is an Open Source PHP class for generating PDF documents.

Its a best way to generate PDF in CakePHP, comparatively any other available helper i ever used.

How to use ?
Below are the steps.

Step: 1 Download and Configure

  1. Go to http://www.tcpdf.org and download the latest version of TCPDF.
  2. Extract to one of your vendors folders, such as app/vendors. It will create a directory tcpdf there with tcpdf.php and more in it. You need at least the folders tcpdf/config and tcpdf/fonts in your application.
  3. Configure TCPDF, see its documentation. You want at least to have a look at tcpdf/config/tcpdf_config.php.
Step 2: Extend TCPDF to customize your header and footer

There is a default header and footer in TCPDF, defined in a header() and a footer() method, which is supposed to be overwritten by you, if needed. This can be done by extending TCPDF and then calling this extended TCPDF class from your application.

In app/vendors create the file xtcpdf.php with this content:

xheadertext = 'YOUR ORGANIZATION';
* set the fill color in the view using
* $fpdf->xheadercolor = array(0,0,100); (r, g, b)
* set the font in the view using
* $fpdf->setHeaderFont(array('YourFont','',fontsize));
*/
function Header() {

list($r, $b, $g) = $this->xheadercolor;
$this->SetY(-290);
$this->SetFillColor($r, $b, $g);
$this->SetTextColor(0, 0, 0);
$this->SetFont('times', '', 14);
$this->writeHTMLCell(-250, '', '', '', $this->xheadertext, 0, 0, false, true, 'C');
$this->SetY(-280);
$this->Cell(0,8, '','T',1,'C');
}

/**
* Overwrites the default footer
* set the text in the view using
* $fpdf->xfootertext = 'Copyright © %d YOUR ORGANIZATION. All rights reserved.';
*/
function Footer() {
$footertext = sprintf($this->xfootertext, $year);
$this->SetY(-20);
$this->SetTextColor(0, 0, 0);
$this->SetFont($this->xfooterfont,'',$this->xfooterfontsize);
$this->Cell(0,8, '','T',1,'C');
$this->SetY(-18);
// write the second column
$this->writeHTMLCell(180, '', '', '', $this->xfootertext, 0, 0, false, true, 'C');
}

}

?>


Step 3: Create your layout for PDF

You cannot use your default layout, as it would wrap the PDF file in your HTML page code. You need a layout such as this one, save it as app/views/layouts/pdf.ctp :


header("Content-type: application/pdf");
echo $content_for_layout;
?>

Step 4: For your Controller

function downloadPdf() {
$this->layout = 'pdf';

Configure::write('debug', 0);
$tcpdf = new XTCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$some_filed = $_REQUEST['some_filed'];
$some_filed = $_REQUEST['some_filed'];
$some_filed = $_REQUEST['some_filed'];
$obj = new SomeHelper();
$Somefiled = ucwords($obj->getSomefiled($brand));
$rowsArray = $obj->SomeFunction($some_filed, $some_filed, $some_filed);
$this->set('tcpdf', $tcpdf);
$this->set('some_filed', $some_filed);
$this->set('rowsArray', $rowsArray[);
$this->render('/reports/procurement/download_pdf');
}
Step 5: For your View

View Template:


$html='';
$slno=1;
$textfont = 'times'; // looks better, finer, and more condensed than 'dejavusans'
//set margins
$tcpdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$tcpdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$tcpdf->SetFooterMargin(PDF_MARGIN_FOOTER);
//set auto page breaks
$tcpdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$tcpdf->setHeaderFont(array($textfont, '', 18));
$tcpdf->xheadercolor = array(255, 255, 255);
$tcpdf->xheadertext = "

Header of the page

";

$tcpdf->xfootertext = "Copy Right Message

";


// add a page (required with recent versions of tcpdf)
$tcpdf->AddPage(); // Front Page

$html_1 = "

$supplier

";


$tcpdf->SetY(-270);
$tcpdf->SetTextColor(0, 0, 0);
$tcpdf->SetFont('times', '', 12);
$tcpdf->writeHTMLCell(-250, '', '', '', $html_1, 0, 0, false, true, 'C');

$curNtDateTime = explode(' ', Date('d-m-Y H:i:s'));
$curDate = $curNtDateTime[0];
$curTime = $curNtDateTime[1];



$lefthtml = "Date: $curDate";

$tcpdf->SetY(-260);
$tcpdf->SetTextColor(0, 0, 0);
$tcpdf->SetFont('times', '', 12);
$tcpdf->writeHTMLCell(-250, '', '', '', $lefthtml, 0, 0, false, true, 'L');


$righthtml = "Time: $curTime";

$tcpdf->SetY(-260);
$tcpdf->SetTextColor(0, 0, 0);
$tcpdf->SetFont('times', '', 12);
$tcpdf->writeHTMLCell(-250, '', '', '', $righthtml, 0, 0, false, true, 'R');

$html = "









";
foreach ($rowsArray as $row) {

$html.= '';
$html.= '';
$html.= '';
$html.= '';
$html.= '';
$html.= '';
}

$html.="
S. Nosome_filedsome_filedsome_filedsome_filedsome_filed
' . $slno++ . '' . $row['some_filed'] . '' . $row['some_filed'] . '' . (int) $row['some_filed'] . '' . $row['some_filed'] . '' . $row['mrp'] . '
";



$tcpdf->setY(-240);
$tcpdf->SetTextColor(0, 0, 0);
$tcpdf->SetFont($textfont, '', 12);

// output the HTML content
$tcpdf->writeHTML($html, true, false, true, false, '');

// reset pointer to the last page
$tcpdf->lastPage();

echo $tcpdf->Output('File_Name.pdf', 'D');
?>


So is it done?
Yeah.. Now you are done ;)
Enjoy!!
References:
http://bakery.cakephp.org/articles/kalileo/2010/06/08/creating-pdf-files-with-cakephp-and-tcpdf

0 comments:

Post a Comment