Skip to main content

How to Create a PDF file in Cakephp 2.0 using Fpdf


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
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
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:
Step 6: Create the view /app/View/[view_name]/pdf.ctp
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


Comments

  1. 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
    fopen(/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

    ReplyDelete
  2. 1xbet korean | Legalbet.co.kr
    1xbet korean.com. 1xbet korean Is this legal febcasino in South Korea? · Top 5 best betting sites, 바카라 online betting sites, sportsbook, poker room,

    ReplyDelete

Post a Comment

Popular posts from this blog

Setup CakePHP Using Xampp On Windows

Step 1: Download XAMPP  and  CakePHP .   Step 2: Install Xampp Once you have installed Xampp (version 1.7.3) on your Windows with the default option, all your files will be located in the C:\xampp folder. Step 3: Mod Rewrite Module Once Xampp is installed as the local server, you can then proceed to enable mod_rewrite. To do so, you will have to open the httpd.conf file that is located in C:\xampp\apache\conf and uncomment by removing # from the following line: # LoadModule rewrite_module modules/mod_rewrite.so Step 4: Place CakePHP Files in a New Folder Extract the CakePHP (version 1.3.8) zip file and copy all its contents to your local web server, which in this instance is C:\xampp\htdocs\cakephp . I have decided to name the CakePHP folder as cakephp, and in it, you will find many files and folders for the framework, including app, cake, docs, vendors, .htaccess, and index.php. Step 5: Set Up Virtual Host Open the httpd-vhosts.conf file from th...
What is Cakephp Actually ? CakePHP is a PHP Rapid Development Framework. PHP Framework? What is it? Why should I rely on a Framework? PHP developers are mostly self-trained (at least I am so) and being rather easy-to-develop, lots of developers jump-start developing applications without caring for code organisation/optimization and security.So one should know before you start with the exact application.It becomes a real challenge to DEBUG an unorganized code! Basically we need to follow up some conventions.Since to maintain a proper format of coding.So that it can be understood by any one who refers the code . CakePHP does that. It sets few CONVENTIONs for following and also perform a lot of tasks automatically - like generating a web form from database tables (with ALL VALIDATION RULES), integrating AJAX (Yes! It does so! Even if you do not have much understanding of AJAX), creating sophisticated SQL queries (master-detail type even), and it does so in a bette...

Installing Wamp on Windows

WAMP is an abbreviated name for the software stack Windows, Apache, MySQL, PHP. It is  derived from LAMP which stands for Linux, Apache, MySQL, and PHP. As the name implies, while LAMP is used on Linux servers, WAMP is used on Windows servers.  The “A” in WAMP stands for Apache.  Apache  is server software that is used to serve webpages. Whenever someone types in your WordPress website’s URL, Apache is the software that “serves” your WordPress site. The “M” in WAMP stands for MySQL.  MySQL  is a database management system. It’s job in the software stack is to store all of your website’s content, user profiles, comments, etc. The “P” in WAMP stands for PHP. PHP is the programming language that WordPress is written in. It is also the piece that holds the entire software stack together. It runs as a process in Apache and communicates with the MySQL database to dynamically build your webpages. Download the wamp for the url  http://www.wam...