使用 FPDF 循环处理多个 PDF

Multiple PDFs in Loop with FPDF

我有一个 FDPF 功能,可以为网站的各个部分创建标准的 PDF。它只是获取一些数据,创建多页 PDF 和 returns PDF 对象,例如

function pdfBuild($orderData) {



class PDF extends FPDF
{
// Page header
function Header()
{
    // Logo
    $this->Image('logo.png',10,6,30);
    // Arial bold 15
    $this->SetFont('Arial','B',15);
    // Move to the right
    $this->Cell(80);
    // Title
    //$this->Cell(80,10,'Game Sheet',1,0,'C');
    // Line break
    $this->Ln(20);
}

// Page footer
function Footer()
{
    // Position at 1.5 cm from bottom
    $this->SetY(-15);
    // Arial italic 8
    $this->SetFont('Arial','I',8);
    // Page number
    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}



}

$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->SetFont('Times','',12);
   for($a=0;$a<count($orderData);$a++) {
    $pdf->AddPage();
    //Add stuff to PDF
   }
return $pdf;

}

我的问题出现在一个将单个 PDF 通过电子邮件发送给客户的循环中,例如

function buildPDFs() {

    //Get a location
    $query = "GET SOME CLIENT INFORMATION";
    $result = getSQLConnection($query);
    if ($result->num_rows > 0) {

            while($row = $result->fetch_assoc()) {

                //Get some info from dB using $row

                //Format info from db into $data variable

                $pdf = pdfBuild($data);

                //Test Data
                $to = 'Whosebug@question.com';
                $from = 'hello@clientinfo.com';
                $subject = 'PDFs'
                $message = 'Howdy';
                //End
                emailPDF($pdf, $to, $from, $subject, $message);

            }
    }

    echo 'Done';

}

问题是,如果我执行两次循环迭代,附件是第一次生成的 PDF,但我收到两封附件相同的电子邮件。因此,看起来 $pdf 对象在循环中永远不会改变,即使我确定每次传递给 PDF 生成函数的数据都是不同的。生成的 PDF 是相同的。

我是否需要在循环中每次都取消设置或以其他方式 'destroy' PDF 对象?

更新

删除页眉和页脚的 Class 定义,如上面的代码片段,解决了这个问题。但是我不明白为什么。

问题:为什么去掉'class extend'代码就解决了问题,让循环每次生成不同的PDF,如预期的那样,并正确地通过电子邮件发送?

下面来自 Veve 的回答并解决了问题。我错误地声明并完全滥用了 Class

Why does the removal of the 'class extend' code solve the problem and allow the loop to generate different PDFs each time, as expected, and email them correctly?

这是因为您混合了函数声明和其中的 class。

要解决它,首先使用扩展 class 创建 pdf.php,它只会覆盖所需的方法:

require_once('fpdf.php');

class PDF extends FPDF
{
    // Page header
    function Header()
    {
        // Logo
        $this->Image('logo.png',10,6,30);
        // Arial bold 15
        $this->SetFont('Arial','B',15);
        // Move to the right
        $this->Cell(80);
        // Title
        //$this->Cell(80,10,'Game Sheet',1,0,'C');
        // Line break
        $this->Ln(20);
    }

    // Page footer
    function Footer()
    {
        // Position at 1.5 cm from bottom
        $this->SetY(-15);
        // Arial italic 8
        $this->SetFont('Arial','I',8);
        // Page number
        $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
    }
}

之后,在您的 code.php 中,您可以将此 class 与您用于在其自身函数中生成 PDF 的代码一起使用:

require_once('pdf.php');

function pdfBuild($orderData) {
    $pdf = new PDF();
    $pdf->AliasNbPages();
    $pdf->SetFont('Times','',12);
       for($a=0;$a<count($orderData);$a++) {
        $pdf->AddPage();
        //Add stuff to PDF
       }
    return $pdf;
}

function buildPDFs() {

    //Get a location
    $query = "GET SOME CLIENT INFORMATION";
    $result = getSQLConnection($query);
    if ($result->num_rows > 0) {

            while($row = $result->fetch_assoc()) {

                //Get some info from dB using $row

                //Format info from db into $data variable

                $pdf = pdfBuild($data);

                //Test Data
                $to = 'Whosebug@question.com';
                $from = 'hello@clientinfo.com';
                $subject = 'PDFs'
                $message = 'Howdy';
                //End
                emailPDF($pdf, $to, $from, $subject, $message);

            }
    }

    echo 'Done';

}