如何将 FPDF 总计添加到表格的最后一行?
How to add FPDF totals to the last row of tables?
我有一组按来源分类的 table,例如:
"Client"、"Stock"、"Floorstock"。
它们看起来像这样:
每个都包含价格列表,需要在 table 的最后一行将这些价格相加并合计。
我在将 "totals" 数组链接到 "tables" 数组时遇到问题 - 特别是在每个 table
的末尾显示每个总数
至此,每个table的总和是在另一个函数中生成的,其中sourceTotals
是一个实例数组:
public function setSourceTotalsArray($data)
{
$this->sourceTotals = $data["source_totals"];
return $this->sourceTotals;
}
这个 returns 数组如下所示:
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
对象中构建 table 的结构,以便 table 具有与正确列名相对应的单元格:
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
,因为我目前仅尝试 returns null
.
你的 setSourceTotalsArray
方法实际上没有做任何事情,所以如果它 returns null
这意味着 $data
没有 source_totals
节点。 (Suggestion/sidenote:将 error reporting 设置为 E_ALL
,因此 PHP 会警告您;然后对于实时环境,请关闭 display_errors
并启用 log_errors
,这样您会在日志中看到错误,但访问者不会在屏幕上看到它们。)
$data
中不存在 source_totals
的原因是完整脚本中的 $datavariable inside
orderDetailis only a subset of
$data`。
我要做的是向 orderDetail
添加一个 $total
参数。你会得到这样的东西:
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.
另一种生成 PDF 的绝妙方法是简单地使用 wkhtmltopdf 二进制文件。
如果您已经有生成 html 的报告,您可以使用输出缓冲生成 html 然后转换为 pdf,它可以在大多数环境下 运行 并且您可以精确地控制你想要的输出,就像 WebKit(Chrome、Safari 等)一样,使用熟悉的语言(HTML)。
HTH.
我有一组按来源分类的 table,例如: "Client"、"Stock"、"Floorstock"。 它们看起来像这样:
每个都包含价格列表,需要在 table 的最后一行将这些价格相加并合计。
我在将 "totals" 数组链接到 "tables" 数组时遇到问题 - 特别是在每个 table
的末尾显示每个总数至此,每个table的总和是在另一个函数中生成的,其中sourceTotals
是一个实例数组:
public function setSourceTotalsArray($data)
{
$this->sourceTotals = $data["source_totals"];
return $this->sourceTotals;
}
这个 returns 数组如下所示:
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
对象中构建 table 的结构,以便 table 具有与正确列名相对应的单元格:
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
,因为我目前仅尝试 returns null
.
你的 setSourceTotalsArray
方法实际上没有做任何事情,所以如果它 returns null
这意味着 $data
没有 source_totals
节点。 (Suggestion/sidenote:将 error reporting 设置为 E_ALL
,因此 PHP 会警告您;然后对于实时环境,请关闭 display_errors
并启用 log_errors
,这样您会在日志中看到错误,但访问者不会在屏幕上看到它们。)
$data
中不存在 source_totals
的原因是完整脚本中的 $datavariable inside
orderDetailis only a subset of
$data`。
我要做的是向 orderDetail
添加一个 $total
参数。你会得到这样的东西:
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.
另一种生成 PDF 的绝妙方法是简单地使用 wkhtmltopdf 二进制文件。
如果您已经有生成 html 的报告,您可以使用输出缓冲生成 html 然后转换为 pdf,它可以在大多数环境下 运行 并且您可以精确地控制你想要的输出,就像 WebKit(Chrome、Safari 等)一样,使用熟悉的语言(HTML)。
HTH.