2016-10-17 43 views
0

我有一个FDPF功能,可为网站的各个部分创建标准PDF。它只需要一些数据,创建一个多页PDF并返回PDF对象,例如使用FPDF循环显示多个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 = '[email protected]'; 
       $from = '[email protected]'; 
       $subject = 'PDFs' 
       $message = 'Howdy'; 
       //End 
       emailPDF($pdf, $to, $from, $subject, $message); 

      } 
    } 

    echo 'Done'; 

} 

的问题是,如果我做循环的两个迭代,附件是在生成的PDF第一个例子,但我得到了两封电子邮件与相同的附件。因此,即使我确信每次传递给PDF生成函数的数据都不同,$ pdf对象也不会在循环中发生变化。生成的PDF是相同的。

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

更新

移除页眉和页脚的类定义的,如在上面的代码段中,解决了这个问题。但我不明白为什么。

问题:为什么删除'class extend'代码可以解决问题并允许循环根据预期每次生成不同的PDF并将它们正确发送给他们?

从Veve下面回答并解决问题。我被错误声明和完全滥用类

+0

如果PDF不改变,它建外循环只有一次 – nogad

+0

你在哪里里面定义'$ data'你的功能? – BenM

+0

@nogad,你可以扩展你的答案。我如何构建'一次'? BenM,数据是使用另一个函数构建的。我可以确认它在循环的每次迭代中发生变化并被发送。但PDF对象不会改变。 – Colin

回答

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 = '[email protected]'; 
       $from = '[email protected]'; 
       $subject = 'PDFs' 
       $message = 'Howdy'; 
       //End 
       emailPDF($pdf, $to, $from, $subject, $message); 

      } 
    } 

    echo 'Done'; 

} 
+1

谢谢Veve。很好的答案和作品! – Colin