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.