Skip to content
SlavVv edited this page Dec 19, 2012 · 8 revisions

Category:Libraries::Community

Introduction With the fpdf library found on fpdf.org there is a problem if you want to use it in a loop when you load it as a CI library.

$this->load->library('fpdf');
$filenames =  array('filename1','filename2');
$filecontents = array('content 1','content 2');
$basepath = dirname(FCPATH);

foreach($filenames as $key=>$filename)
{
    $this->fpdf->AddPage();
    $this->fpdf->SetFont('Arial','B',16);
    $this->fpdf->Cell(40,10,$filecontents[$key]);
    $this->fpdf->Output($basepath.'/pdf/'.$filename.'.pdf');
}

This will not have the desired output as the content for the both files will be: content 1. If you extend the fpdf library to add your own header with a logo you will get an error because the fpdf library unsets the image data once it's done with it.

The alteration The alteration is not more than moving the content of the FPDF function to an added initialize function. Which makes it possible to do this;

$this->load->library('fpdf');
$filenames =  array('filename1','filename2');
$filecontents = array('content 1','content 2');
$basepath = dirname(FCPATH);

foreach($filenames as $key=>$filename)
{
    $this->fpdf->initialize('P','mm','A4');
    $this->fpdf->AddPage();
    $this->fpdf->SetFont('Arial','B',16);
    $this->fpdf->Cell(40,10,$filecontents[$key]);
    $this->fpdf->Output($basepath.'/pdf/'.$filename.'.pdf');
}

This code will get you the desired effect. The downside for the moment is that you have to use the intialize function even if you load the library only once. This can be prevented by adding the initialize parameters as an array as the second parameter when you load the library.

$this->load->library('fpdf',array('P','mm','A4'));

This is how libraries like the CI file upload work that is why i called the modified library fpdf CIed

download File:fpdf.zip

Category:Contributions::Libraries::Files

Clone this wiki locally