【问题标题】:Multiple PDFs in Loop with FPDF使用 FPDF 循环播放多个 PDF
【发布时间】:2017-02-26 22:06:26
【问题描述】:

我有一个 FDPF 函数,可以为网站的各个部分创建标准 PDF。它只是接收一些数据,创建一个多页 PDF 并返回 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 = 'stackoverflow@question.com';
                $from = 'hello@clientinfo.com';
                $subject = 'PDFs'
                $message = 'Howdy';
                //End
                emailPDF($pdf, $to, $from, $subject, $message);

            }
    }

    echo 'Done';

}

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

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

更新

去掉Header和Footer的Class定义,如上面代码sn-p,问题解决。但我不明白为什么。

问题:为什么删除“类扩展”代码可以解决问题并允许循环按预期每次生成不同的 PDF 并正确通过电子邮件发送?

从 Veve 回答下面的问题并解决问题。我错误地声明并完全滥用了这个类

【问题讨论】:

  • 如果 pdf 没有改变,只在循环外构建一次
  • 你在哪里定义$data在你的函数里面?
  • @nogad,您能否扩展您的答案。我如何“一次”构建它? BenM,数据是使用另一个函数构建的。我可以确认它在循环的每次迭代中发生变化并被发送。然而,PDF 对象并没有改变。
  • $pdf = pdfBuild($data); 移动到循环开始之前,当前您为每次发送重新创建相同的 pdf。
  • 感谢@nogad,但我在每次迭代中都使用来自 dB 的 $row 更改 $data。我已经更新了上面的代码以反映这一点。看起来函数 pdfBuild 总是返回相同的 $pdf。

标签: php pdf fpdf


【解决方案1】:

为什么删除“类扩展”代码可以解决问题? 允许循环按预期每次生成不同的 PDF,并且 正确地给他们发电子邮件?

这是因为你在其中混合了一个函数和一个类的声明。

要解决它,首先使用您的扩展类创建 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 中,您可以使用这个类,以及用于在其自己的函数中生成 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 = 'stackoverflow@question.com';
                $from = 'hello@clientinfo.com';
                $subject = 'PDFs'
                $message = 'Howdy';
                //End
                emailPDF($pdf, $to, $from, $subject, $message);

            }
    }

    echo 'Done';

}

【讨论】:

    猜你喜欢
    • 2018-01-16
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    • 2018-08-15
    相关资源
    最近更新 更多