Creating PDF with Codeigniter using FPDF library
anoopsachari | Apr 16, 2011 | Comments 3
Generating PDF reports with your application would be a heavy task. I had googled but couldn’t find a good document that explains the same.
However i successfully integrated FPDF with my php application. I would share step-by-step info on how to do it.
I hope you would know the basics on CodeIgniter. Download a copy of CI from its official site.
Step 01 : Download FPDF Fonts from here. Extract the – fpdf_fonts – folder to root folder of your CI project.
Step 02 : Download fpdf library from here. Extract – fpdf – folder into system\application\libraries\
Step 03 : Create a file named init_fpdf.php under system\application\libraries\
init_fpdf.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
if ( ! class_exists('fpdf')){
require_once(BASEPATH.'libraries/fpdf'.EXT);
}
$obj =& get_instance();
$obj->fpdf = new fpdf();
$obj->ci_is_loaded[] = 'fpdf';
?>
You are done with the configuration. Now into Controller.
Step 03 : The code within your controller would be. Say for Example the controller function name is PDF.
function PDF()
{
$this->load->helper('path');
$font_directory = './fpdf_fonts/';
set_realpath($font_directory);
$this->load->library('fpdf');
define('FPDF_FONTPATH',$font_directory);
$this->fpdf->Open();
$this->fpdf->AddPage();
$this->fpdf->SetFont('Arial','',8);
$this->fpdf->Cell(80);
$this->fpdf->Cell(0,0,'Hello FPDF',0,1,'R');
$this->fpdf->Output();
}
This would output a PDF for you ! You are done !
If you need to save the PDF file into a folder and get its filename you would need to add some more code.
Drop me a comment it you need that functionality also, would explain it to you.
Filed Under: codeigniter
About the Author: a holistic web developer , movie buff and technical blogger from queen of arabian sea.







This is great! I was just thinking about this issue this week. Thank you for the post!
tcPDF is a much cleaner implementation of fPDF with greater support for direct from HTML pdf generation. I recently upgraded an old fPDF implementation to tcPDF and it was quite painless.
Hi
I will need to save the file to a folder on the server and email that pdf file as well. Would you be able to get me the code for that as well.
Thank you