|
|

|
andolasoft
Level 1 User
Occupation: Ruby on Rails Developer
Location: San Jose, California
Offline
|
Joined: Aug 03, 2010
Find all post by andolasoft
Total Posts: 3
0.00 posts per day
Total Words: 445
|
Posted: Fri Aug 26, 2011 2:48 am Post subject: How to Generate PDF file in CakePHP
|
|
|
|
|
TCPDF is a free and open source software one of the widely used PHP libraries in the world. This is because of the fact that it already included in the most popular PHP-based CMS and applications including Cakephp.
The installation is pretty straight forward and easy-to-use in Cakephp Framework. Many web applications use this as output documents like invoices, contracts or just web pages in the PDF format.
Following are the steps to integrate TCPDF in Cakephp MVC framework.
Step 1:
Go to http://www.tcpdf.org and download the latest version of TCPDF zip file.
Then unzip the zip file and save under the Vendor folder in cakephp framework(app\vendors)
This creates a directory tcpdf there with tcpdf.php and more in it (app\vendors\tcpdf)
You can configure the PDF file Like header Logo Image, Page Title, page Margin etc. in the TCPDF configure file (app->vendors->tcpdf-> tcpdf_config.php)
Step 2:
You can create your own header and footer page of your PDF file. Create a page “xtcpdf.php” under app/vendors with these contents as shown below.
App::import('Vendor','tcpdf/tcpdf');
class XTCPDF extends TCPDF
{
var $xheadertext = 'PDF created using CakePHP and TCPDF';
var $xheadercolor = array(0,0,200);
var $xfootertext = 'Copyright © %d XXXXXXXXXXX. All rights reserved.';
var $xfooterfont = PDF_FONT_NAME_MAIN ;
var $xfooterfontsize = 8 ;
/* Change header text and font size as per your requirement in the above variable*******/
function Header()
{
list($r, $b, $g) = $this->xheadercolor;
$this->setY(10); // shouldn't be needed due to page margin, but helas, otherwise it's at the page top
$this->SetFillColor($r, $b, $g);
$this->SetTextColor(0 , 0, 0);
$this->Cell(0,20, '', 0,1,'C', 1);
$this->Text(15,26,$this->xheadertext );
}
function Footer()
{
$year = date('Y');
$footertext = sprintf($this->xfootertext, $year);
$this->SetY(-20);
$this->SetTextColor(0, 0, 0);
$this->SetFont($this->xfooterfont,'',$this->xfooterfontsize);
$this->Cell(0,8, $footertext,'T',1,'C');
}
}
?>
Step 3:
Create your layout under app/views/layouts/pdf.ctp;
header("Content-type: application/pdf");
echo $content_for_layout;
?>
Step 4:
Here is the Controller code which will display output code of generating PDF file;
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:
Create a page under your view directory (app/views/) named as “view_pdf.ctp” (this name can be change as per your controller method) and write your HTML code/PHP code.
Please contact us at info@andolasoft.com in case you
need help to complete Step# 5. Thanks.
|
|
|
|
|
Back to top
Sponsor Advertising
|
|
|
|
|

|
TimMCQuinn
Team-Member
Occupation: Web Developer
Location: USA
Online
|
Joined: Jan 03, 2007
Total Posts: 6,126
10.5 post per day
Total Words: 510,56
|
Posted: Fri Aug 26, 2011 2:48 am Post subject: This ia a great site
|
|
|
|
|
Hey!, Check out these new links I think you may find them intresting and relevant to what your looking for....
Forum Signature Need web hosting or blog hosting try VisionThisHosting.com their the best!
|
|
|
|
|
|
|
|
Back to top
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|