代码之家  ›  专栏  ›  技术社区  ›  hello world

如何将FPDF总计添加到表格的最后一行?

  •  6
  • hello world  · 技术社区  · 7 年前

    它们看起来像这样:

    List of Floor Stock orders

    每个都包含价格列表,需要在表格的最后一行进行相加和汇总。

    我在将“totals”数组链接到“tables”数组时遇到了问题,特别是在每个表的末尾显示每个总计

    到目前为止,每个表的总计是在另一个函数中生成的,其中 sourceTotals 是实例阵列:

    public function setSourceTotalsArray($data)
    {
        $this->sourceTotals = $data["source_totals"];
        return $this->sourceTotals;
    }
    

    返回如下数组:

    array(4) { ["Floorstock"]=> int(0) ["Stock"]=> int(0) ["Client"]=> float(32.18)  } 
    

    这是我定义我的 $pdf 对象:

    if($_SERVER["REQUEST_METHOD"] == "POST"){
    $data = json_decode($_POST["printData"],true);
    $pdf = new PDF();
    // Column headings
    $month = $pdf->getMonth($data['month']);
    $year = $data['year'];
    $pdf->SetFont('Arial','',10);
    $pdf->AddPage();
    $pdf>title(array('month'=>$month,'year'=>$year,'showroom'=>ucfirst($data['showroom_name'])));
    $pdf->tableBody($data);
    
    if(!empty($data["order_detail"])){
        $customerOrders = array();
        $stockOrders = array();
        $floorstockOrders = array();
        $otherOrders = array();
    
        foreach($data["order_detail"] as $o){
            switch($o["orderSource"]){
                case 'Client':
                    $customerOrders[] = $o;
                    break;
                case 'Stock':
                    $stockOrders[] = $o;
                    break;
                case 'Floorstock':
                    $floorstockOrders[] = $o;
                    break;
    
                default:
                    $otherOrders[] = $o;
                    break;
            }
        }
    
        if (!empty($customerOrders)) {
            $pdf->orderDetail($customerOrders, $data['currency'], 'Client');
        }
    
        if (!empty($stockOrders)) {
            $pdf->orderDetail($stockOrders, $data['currency'], 'Stock');
        }
    
        if (!empty($floorstockOrders)) {
            $pdf->orderDetail($floorstockOrders, $data['currency'], 'Floor Stock');
        }
    
        if (!empty($otherOrders)) {
            $pdf->orderDetail($otherOrders, $data['currency'], 'Client');
        }
    }
    $pdf->Output();
    

    这个 orderDetail 函数是在 $pdf 对象,使表中的单元格与正确的列名相对应:

    function orderDetail($data,$currencyShortCode, $type){
        $this->orderType = $type;
        list($currencySymbol, $w_symbol, $cellHight) = $this->getOrderDetailHeader($currencyShortCode, $type);
    
        foreach ($data as $d){
            $commaValue = $this->addComma((float)$d['value']);
            $this->Cell(15,$cellHight,$d['order_no'],'LTB',0,'L');
            $this->Cell(20,$cellHight,substr($d['customer'],0,13),'TB',0,'L');
            $this->Cell(20,$cellHight,substr($d['company'],0,13),'TB',0,'L');
            $this->Cell(20,$cellHight,substr($d['ref'],0,13),'TB',0,'L');           
            $this->Cell(2,$cellHight,'','TB',0,'R');
            $this->Cell($w_symbol,$cellHight,$currencySymbol,'TB',0,'R');
            $this->Cell(16-$w_symbol,$cellHight,$commaValue,'TB',0,'R');
            $this->Cell(2,$cellHight,'','TB',0,'R');
            $this->Cell(10,$cellHight,$d['cat'],'TB',0,'L');
            $this->Cell(84,$cellHight,$d['description'],'RTB',0,'L');
            $this->Ln($cellHight);
        }
        //BOTTOM TOTAL
        $this->Cell(13,$cellHight,'TOTAL','LRTB',0,'L');
        $this->Cell(22,$cellHight,'','TB',0,'L');
        $this->Cell(20,$cellHight,'','TB',0,'L');
        $this->Cell(20,$cellHight,'','TB',0,'L');
        $this->Cell(4,$cellHight,$currencySymbol,'TB',0,'R');
        $this->Cell(14,$cellHight,$this->setSourceTotalsArray($data),'TB',0,'R'); //HERE
        $this->Cell(2,$cellHight,'','TB',0,'R');
        $this->Cell(10,$cellHight,'','TB',0,'L');
        $this->Cell(84,$cellHight,'','RTB',0,'L');
    }
    

    我只是不知道该怎么通过 setSourceTotalsArray 进入 订单详细信息 ,因为我当前的尝试只返回 null .

    2 回复  |  直到 7 年前
        1
  •  5
  •   Jory Geerts    7 年前

    你的 setSourceTotalsArray 方法实际上什么都不做,所以如果它返回 null 这意味着 $data 没有 source_totals error reporting E_ALL 所以PHP会警告你这一点;然后,对于实时环境 display_errors 关闭并启用 log_errors 原因 source_总计 中不存在 $数据 这是$数据吗 variable inside is only a subset of $data`在完整脚本中。

    我要做的是添加一个 $total orderDetail . 你会得到这样的结果:

    function orderDetail($data,$currencyShortCode, $type, $total) {
        ...
        $this->Cell(14,$cellHight, $total,'TB',0,'R'); //HERE
        ...
    }
    

    if (!empty($customerOrders)) {
        $pdf->orderDetail($customerOrders, $data['currency'], 'Client', $data['source_totals']['Client']);
    }
    // Also add the 4th argument for stock orders, floor orders and others.
    
        2
  •  2
  •   Felipe Valdes    7 年前

    如果您已经有了一个生成html的报告,您可以使用输出缓冲来生成html,然后转换为pdf,它可以在大多数环境中运行,您可以准确地控制所需的输出,就像WebKit(Chrome、Safari等)使用熟悉的语言(html)那样。

    嗯。