Multiple PDFs in Loop with FPDF

怎甘沉沦 提交于 2020-02-07 12:21:56

问题


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;

}

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

}

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.

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

Update

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

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?

Answer below from Veve and solves the problem. I was mis-declaring and completely misusing the Class


回答1:


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.

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

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

}


来源:https://stackoverflow.com/questions/40094584/multiple-pdfs-in-loop-with-fpdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!