我有一组按来源分类的表格,例如: “客户”,“股票”,“Floorstock”。 它们看起来像这样:
每个都包含价格清单,需要在表格的最后一行加起来并总计。
我在将“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
传递给orderDetail
,因为我当前的尝试只返回null
。
答案 0 :(得分:4)
您的setSourceTotalsArray
方法实际上并没有做任何事情,因此如果它返回null
,则表示$data
没有source_totals
节点。 (建议/旁注:将error reporting设置为E_ALL
,以便PHP向您发出警告;然后在实时环境中关闭display_errors
并启用log_errors
,这样您就会看到错误在日志中,但访问者不会在屏幕上看到它们。)
source_totals
中不存在$data
的原因是完整脚本中的$ 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.
答案 1 :(得分:2)
另一种生成PDF的好方法就是使用wkhtmltopdf二进制文件。
如果您已经有一个生成html的报告,您可以使用输出缓冲生成html,然后转换为pdf,它可以在大多数环境中运行,您可以精确控制所需的输出,就像WebKit(Chrome, Safari等,使用熟悉的语言(HTML)来做。
HTH。