logo logo

Easily integrate ezPDF (a.k.a pdf-php) into CodeIgniter Framework

Got another spare time, so, another simple tips for you. Last post I already show how to easily integrate PHPExcel in CodeIgniter so you’ll be able to create downloadable XLS report easily. We will complete our CodeIgniter reporting capability by adding PDF output feature. (Yeah, I know PHPExcel can also create a PDF output, but it sucks big, the table is badly formatted)

Why ezPDF (now pdf-php)?

Well, there’s already a guide for integrating TCPDF and DOMPDF into CodeIgniter so I present you another option by using ezPDF (pdf-php)

What’s ezPDF not good for?

Don’t use ezPDF if you are planning to use CSS or HTML codes to style your PDF. As far as I know, ezPDF does not offer easy way to parse CSS or HTML (more like it doesn’t support it). I’m not saying that you can’t style your text, in fact, some primitive text styling is supported, such as: <b>,<i>,<u>. Please read ezPDF readme file to comprehend what ezPDF has and hasn’t.

What’s ezPDF good at?

Graphic functionality, drawing (line, curve, ellipse, box, polygon), advance page numbering, paging function, automatic table-of-content generation. (On my test, ezPDF/pdf-php failed to insert a PNG image with transparency/alpha-channel rendering corrupted PDF file)

  • Create pdf-php folder in your application/third_party/ folder (let’s separate it from PHPExcel). Extract fonts folder, class.pdf.php and class.ezpdf.php inside your pdf-php folder.
Files and folder need to be extracted from ezPDF

You only need to extract these files and folder

Your application/third_party/ folder will look like this

codeigniter application/third_party structure

codeigniter application/third_party/pdf-php/ structure with ezPDF .zip content extracted there

  • Create Pdf.php inside your application/libraries/. We will name our library as Pdf class. Here’s the full library code:
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
require_once(APPPATH."/third_party/pdf-php/class.ezpdf.php"); 
 
class Pdf extends Cezpdf { 
    public function __construct($params) { 
    	if (is_array($params)) {
			parent::__construct($params['paper'],$params['orientation'], $params['type'], $params['options']); 
    	} else {
			parent::__construct();
    	}
 
    } 
}

Your application/libraries/ folder will look like this:

codeigniter application/libraries/ structure

codeigniter application/libraries/ structure with our new ezPDF/pdf-php library

  • It’s done. This is example on how to use our new ezPDF/pdf-php library (this code is inside a function within a controller):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public function index()
	{
		error_reporting(0);  //suppress some error message
		$parameters=array(
			'paper'=>'letter',   //paper size
			'orientation'=>'landscape',  //portrait or lanscape
			'type'=>'color',   //paper type: none|color|colour|image
			'options'=>array(0.6, 0.9, 0.8) //I specified the paper as color paper, so, here's the paper color (RGB)
		);
		$this->load->library('pdf', $parameters);  //load ezPdf library with above parameters
		$this->pdf->selectFont(APPPATH.'/third_party/pdf-php/fonts/Helvetica.afm');  //choose font, watch out for the dont location!
		$this->pdf->ezText('Hello World!',20);  //insert text with size
 
		//get data from database (note: this should be on 'models' but mehhh..), we'll try creating table using ezPdf
		$q=$this->db->query('SELECT id, username, role FROM administrator');
                //this data will be presented as table in PDF
		$data_table=array();
		foreach ($q->result_array() as $row) {
			$data_table[]=$row;
		}
                //this one is for table header
		$column_header=array(
			'id'=>'User ID',
			'username'=>'User Name',
			'role'=>'Role'
		);
		$this->pdf->ezTable($data_table, $column_header); //generate table
		$this->pdf->ezSetY(480);  //set vertical position
		$this->pdf->ezImage(base_url('images/test_noalpha.png'), 0, 100, 'none', 'center');  //insert image
		$this->pdf->ezStream(array('Content-Disposition'=>'just_random_filename.pdf'));  //force user to download the file as 'just_random_filename.pdf'
	}

Example PDF output will be like this:

codeigniter and ezPDF output result

Example ezPDF output from above codes

On above example, I show you how to:

  • call the library, select paper size, paper orientation, paper color
  • select font type, and font size
  • insert a PNG image into the PDF
  • create a table from database
  • finally, force user to download the PDF file with specific file name

Once again, for more ezPDF/pdf-php features, please read the readme on your ezPDF/pdf-php download.

bottom

4 Responses to “Easily integrate ezPDF (a.k.a pdf-php) into CodeIgniter Framework”

  1. Ravi says:

    hi
    I have used this library with my application. I’m having a problem. when i use utf-8 fonts (Hindi fonts). it is showing some garbage type of data in that column. (Data Comming from mysql table, showing properly on page).
    Will you please help me to fix issue?

  2. dayat says:

    ‘letter’, //paper size
    ‘orientation’=>’landscape’, //portrait or lanscape
    ‘type’=>’color’, //paper type: none|color|colour|image
    ‘options’=>array(0.6, 0.9, 0.8) //I specified the paper as color paper, so, here’s the paper color (RGB)
    );

    $this->load->library(‘pdf’, $parameters); //load ezPdf library with above parameters
    $this->pdf->selectFont(APPPATH.’/third_party/pdf-php/fonts/Helvetica.afm’); //choose font, watch out for the dont location!
    $this->pdf->ezText(‘Hello World!’,20); //insert text with size

    //get data from database (note: this should be on ‘models’ but mehhh..), we’ll try creating table using ezPdf

    $q = $this->db->query(‘SELECT kode_program_studi, nama_program_studi, kode_jenjang_pendidikan FROM pdpt_tmst_program_studi’);

    //this data will be presented as table in PDF
    $data_table = array();

    foreach ($q->result_array() as $row) {
    $data_table[]=$row;
    }

    //this one is for table header
    $column_header = array(
    ‘kode_program_studi’=>’Kode Progdi’,
    ‘nama_program_studi’=>’Nama Progdi’,
    ‘kode_jenjang_pendidikan’=>’Kode Jenjang’
    );

    $this->pdf->ezTable($data_table, $column_header); //generate table
    $this->pdf->ezSetY(480); //set vertical position
    $this->pdf->ezImage(base_url(‘images/test_noalpha.png’), 0, 100, ‘none’, ‘center’); //insert image
    $this->pdf->ezStream(array(‘Content-Disposition’=>’just_random_filename.pdf’)); //force user to download the file as ‘just_random_filename.pdf’
    }
    }

    ?>

    Nothing appear, why?

  3. Davids says:

    i’m using this ezpdf class and is able to create table using the data from mysql database table. the problem is, i’m unable to insert image using either addImage, addJpegFromFile or even addPngFromFile. the code fails to generate pdf if i use any of these 3 classes but when i comment them out, it just runs. what might be the problem?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

bottom