使用FPDF循环处理多个PDF [英] Multiple PDFs in Loop with FPDF

查看:67
本文介绍了使用FPDF循环处理多个PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有FDPF功能,可为网站的各个部分创建标准PDF.它只是吸收一些数据,创建多页PDF并返回PDF对象,例如

I have a FDPF function that creates a standard PDF for various parts of the website. It simply takes some data in, creates a multi page PDF and returns the PDF object e.g.

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发送给客户,例如

My issue arises in a loop that emails individual PDFs out to clients e.g

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是相同的.

The problem is that if I do two iterations of the loop, the attachment is the PDF that was generated in the first instance but I get two emails with the same attachment. As such it would appear the $pdf object never changes in the loop, even though I am certain the data being passed to the PDF generation function each time is different. The generated PDFs are identical.

每次循环时是否需要取消设置或以其他方式销毁" PDF对象?

Do I need to unset or otherwise 'destroy' the PDF object each time in the loop?

更新

删除上面的代码段中的Header和Footer的类定义可以解决此问题.但是我不明白为什么.

The removal of Class definition of Header and Footer, as in the above code snippet, solves the problem. But I can't understand why.

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

Question: 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?

下面从Veve回答并解决了问题.我是在错误地声明并完全滥用了课程

推荐答案

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

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?

这是因为您在其中混合了函数的声明和类.

It's because you're mixing the declaration of a function and a class in it.

要解决该问题,请首先使用扩展类创建 pdf.php ,该扩展类仅覆盖所需的方法:

To resolve it, first create pdf.php with your extended class, which only override the desired methods:

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的代码,该函数具有自己的功能:

After that, in your code.php you can then use this class, with the code you use for generating the PDF in its own function:

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';

}

这篇关于使用FPDF循环处理多个PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆