Step 1: Download FPDF folder from
http://www.fpdf.org/ .
Step 2: Unzip the downloaded Fpdf file and name it “fpdf” or however you require and make sure that you use the same name while calling it.
Step 3: Move the “fpdf” unzipped files to your /app/Vendor directory within Cakephp. Now you should have the directory path as
/app/Vendor/fpdf.
Step 4: Create a new Cakephp layout file for the
pdfs. We will use this layout when serving a pdf to the client. Create a
file called pdf.ctp inside of /app/View/Layouts. Add the following code
to
/app/View/Layouts/pdf.ctp
|
<?php
header('Content-Disposition: attachment; filename="downloaded.pdf"');
echo $content_for_layout;
?>
|
The header function above tells the browser that it is going
to receive a file called download.pdf. If you want to change the name of
the downloaded file, you can replace download.pdf above.
Step 5: Add the viewpdf function to the Controller
/app/Controller/[controller_name]Controller.ctp
|
public function viewpdf() {
App::import('Vendor', 'Fpdf', array('file' => 'fpdf/fpdf.php'));
$this->layout = 'pdf'; //this will use the pdf.ctp layout
$this->set('fpdf', new FPDF('P','mm','A4'));
$this->set('data', 'Hello, PDF world');
$this->render('pdf');
}
|
Add the above function to the controller of your choice. Below I
included a duplicate of the code above with comments to help you
understand each line:
1
2
3
4
5
6
7
8
9
10
11
12
|
public function viewpdf() {
//Import /app/Vendor/Fpdf
App::import('Vendor', 'Fpdf', array('file' => 'fpdf/fpdf.php'));
//Assign layout to /app/View/Layout/pdf.ctp
$this->layout = 'pdf'; //this will use the pdf.ctp layout
//Set fpdf variable to use in view
$this->set('fpdf', new FPDF('P','mm','A4'));
//pass data to view
$this->set('data', 'Hello, PDF world');
//render the pdf view (app/View/[view_name]/pdf.ctp)
$this->render('pdf');
}
|
Step 6: Create the view /app/View/[view_name]/pdf.ctp
|
<?php
$fpdf->AddPage();
$fpdf->SetFont('Arial','B',16);
$fpdf->Cell(40,10,$data);
$fpdf->Output();
?>
|
Paste the above code in the View corresponding to the controller with the viewpdf function defined earlier and alter the section based on your requirement. Here we are just printing out the "Hello, PDF world" which is set from the Controller.
Step 7: View PDF via the url: [site_home]/[controller_name]/viewpdf
If you are still having problems, try adding the below code to the /app/Config/routes.php file
|
Router::parseExtensions('pdf');
|
very good information. it worked perfectly but I have some problems to show images that I have in webroot / img shows me the following error
ReplyDeletefopen(/img/imagen1.png): failed to open stream: No such file or directory [APP\Vendor\fpdf\fpdf.php, line 1232]FPDF error: Can't open image file: /img/imagen1.png
I invoke it in the following way.
$pdf->Image( '/img/imagen1.png',60,30,90,0,'PNG');
You know what is the correct way to invoke it using cakephp. because the routes still confuse me
how to save in directory
ReplyDelete1xbet korean | Legalbet.co.kr
ReplyDelete1xbet korean.com. 1xbet korean Is this legal febcasino in South Korea? · Top 5 best betting sites, 바카라 online betting sites, sportsbook, poker room,