2017-10-09 149 views
7

我有一組由它們的源e.g分類表: 「客戶」,「股票」,「Floorstock」。 他們看起來像這樣:如何將FPDF總計添加到表的最後一行?

List of Floor Stock orders

每個包含的價格列表,其中需要添加並總計表的最後一行。

我在鏈接一個「總計」數組的「表」陣列的問題 - 每次在總

到目前爲止,在另一個函數生成每個表的總計每個表的末尾具體顯示,其中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'); 
} 

我只是不知道如何通過setSourceTotalsArrayorderDetail,因爲我現在的願望只能返回null

回答

5

您的setSourceTotalsArray方法沒有做任何事情,所以如果它返回null那意味着$data沒有source_totals節點。 (建議/旁註:設置error reportingE_ALL所以PHP會警告你這一點;然後現場環境轉display_errors關閉,使log_errors所以你會看到在日誌中的錯誤,但你的訪問者不會看到它們在屏幕上。) 原因source_totals$data不存在的是,$數據variable inside的完整劇本的OrderDetail 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

另一種很好的方式來生成PDF是簡單地使用wkhtmltopdf二進制文件。

如果您已經有一份報告,它會生成html,您可以使用輸出緩衝來生成html,然後轉換爲pdf,它可以在大多數環境中運行,並且可以像WebKit一樣控制輸出,Safari等)使用熟悉的語言(HTML)進行操作。

HTH。

相關問題