如果我们在循环之外创建一个对象,并不断向多个文档插入相同的图像,则会发现未定义的索引错误。
FPDF库在第一个文档中使用图像后会在内部删除图像。每当循环执行第二条记录以创建文档时,所附的图像都不可用。
因此,错误在FPDF类中抛出。如果我们在循环中移动这个对象,这个未定义的索引错误就会得到解决,但从性能角度来看,这是不断创建新对象的一个缺点。
复制步骤
<?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');
}
如果我们在第1659&1660年也是第三个&第4个PDF文档不正确
function _putimages()
{
foreach(array_keys($this->images) as $file)
{
$this->_putimage($this->images[$file]);
//unset($this->images[$file]['data']);
//unset($this->images[$file]['smask']);
}
}
请帮我解决这个问题。提前谢谢。
试试这个:
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']);
}
}
我在循环中更改了对象创建,它在测试文件中运行良好。但如果我集成了代码点火器,它不起作用,我必须检查
<?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');
}