1

If we create an object outside of the loop and keep inserting the same image to multiple documents the undefined index error was identified.

FPDF library internally deleting the image once it's used in the first document. Whenever the loop executes the second record to create the document the attached image is not available.

Because of this the error is throwing in FPDF class. If we move this object inside the loop this undefined index error is resolved but the performance wise this is a drawback to keep creating new object.

STEPS TO REPRODUCE

<?php 

require 'fpdf.php'; 

$pdf = new FPDF(); 

foreach(array(1,2,3,4) as $value) 
{ 
//add the page to the current PDF 
$pdf->AddPage(); 

$cover_image = 'cover_page.jpg'; 

//add one image to that pdf page 
$pdf->Image($cover_image,0,0,210,'','JPG'); 

$temp = 'temp/'.$value.'.pdf'; 

//output the file into the local folder 
$pdf->Output($temp, 'F'); 

}

If we comment the unset function in FPDF class in line number 1659 & 1660 also the 3rd & 4th PDF document is not proper

function _putimages() 
{ 
    foreach(array_keys($this->images) as $file) 
    { 
        $this->_putimage($this->images[$file]); 
        //unset($this->images[$file]['data']); 
        //unset($this->images[$file]['smask']); 
    } 
} 

Please help me to resolve this issue. Thanks in advance.

Sundar
  • 4,580
  • 6
  • 35
  • 61
  • Invalid PDF files are getting generated (3rd & 4th) while using the above code. Please ignore my changes to the _putimages() function in FPDF class, as i was trying around. We need to create multiple PDFs that use the same cover page image in all the documents. – Sundar Dec 17 '13 at 13:53
  • The notice error is fixed if we move the object creation into inside the loop. Because of existing object is clearing the image once the validation is done – Sundar Dec 17 '13 at 14:15

2 Answers2

0

Try this:

function _putimages() 
{ 
    foreach(array_keys($this->images) as $file) 
    { 
        $this->_putimage($this->images[$file]); 
        if (isset($this->images[$file]['data'])) unset($this->images[$file]['data']); 
        if (isset($this->images[$file]['smask'])) unset($this->images[$file]['smask']); 
    } 
}
Johan
  • 1,958
  • 11
  • 20
  • Actual problem: Invalid PDF files are getting generated (3rd & 4th) while using the above code. Please ignore my changes to the _putimages() function in FPDF class, as i was trying around. – Sundar Dec 17 '13 at 13:51
0

I changed the object creation inside the loop it's working fine in test file.But if I integrate with codeignitor it's not working I have to check that

<?php

require 'fpdf.php';

foreach(array(1,2,3,4) as $value)
{
    $pdf = new FPDF();

    //add the page to the current PDF
    $pdf->AddPage();

    $cover_image = 'cover_page.jpg';

    //add one image to that pdf page
    $pdf->Image($cover_image,0,0,210,'','JPG');

    $temp = 'temp/'.$value.'.pdf';

    //output the file into the local folder
    $pdf->Output($temp, 'F');

}
Sundar
  • 4,580
  • 6
  • 35
  • 61