0

我从订单表创建一个套用信函。每个订单可以有 1 或 2 页。PDF 包含所有订单。现在我想把每个订单的页码放在 PDF 上。

第一顺序:第 1 和 2 页,第二顺序:第 3 页,第三顺序:第 4 页。

页数取决于客户订购的文章数量(最多 2 页)。

PageNo()使用整个文档进行编号。也许有人有同样的问题?

4

1 回答 1

0

FPDF通过子类化 FPDF 的类,并添加一个$orderPageNo属性来跟踪当前订单的“子页码” ,可以达到预期的结果。然后,您可以在自定义HeaderFooter方法中使用该属性。

例子:

<?php

require('fpdf/fpdf.php');

class PDF extends FPDF {

    protected $orderNumber, $orderPageNo;

    function AcceptPageBreak() {
        $accept = parent::AcceptPageBreak();
        if ($accept) {
            $this->orderPageNo++;
        }
        return $accept;
    }

    function Header() {
        $this->Cell(50, 30, 'Order #'.$this->orderNumber);
        $this->Cell(50, 30, 'Page '.$this->orderPageNo, 0, 1);
    }

    function Order($orderNumber, $items) {
        $this->orderNumber = $orderNumber;
        $this->orderPageNo = 1;
        $this->AddPage();

        for ($i = 1; $i <= $items; $i++) {
            $this->Cell(30, 12, 'Item #'.$i, 1, 1);
        }
    }

}

$pdf = new PDF();
$pdf->SetFont('Arial', '', 12);

$orders = array(1 => 15, 2 => 25, 3 => 35);
foreach ($orders as $orderNumber => $items) {
    $pdf->Order($orderNumber, $items);
}

$pdf->Output();
于 2016-09-13T14:55:59.010 回答