我正在使用函数GetStringWidth()
来计算MultiCell()
所需的行数。
列宽为75
,GetStringWidth()
的结果为212
,因此我的文字需要3行才能充分适应我的MultiCell()
。
问题是使用MultiCell()
时此字符串的输出超过4行。然后我意识到这可能是因为输出中的字符串的字间距过大,但没有理由这样做。
使用MultiCell()
输出字符串时,请查看下面的单词间距(您可以在上面的标题中看到正常的字间距):
这是使用Cell()
函数使用相同字符串进行测试时的输出(忽略格式化)。差异很明显:
看来GetStringWidth()
与MultiCell()
无法正确比较,或者MultiCell()
的字间距大于Cell()
。
我尝试过使用多种字体,因此我可以确认这不是字体特定问题。
有人可以帮我确定为什么会发生这种情况吗?我的代码如下。谢谢。
<?php
require('fpdf.php');
$client = new SoapClient('*API URL*');
$session_id = $client->login('*API Username*', '*API Password*');
$order_number = '1000000007';
try {
$result = $client->salesOrderInfo($session_id, $order_number);
$products = (array) $result->items;
$pdf = new FPDF();
$pdf->AddPage();
/* Title - Company Name */
$pdf->SetFont('Helvetica','B',22);
$pdf->Cell(0,10,'Hammer Nutrition',0,1,'C');
/* Sub-Title - "Picking List */
$pdf->SetFont('Helvetica','',16);
$pdf->Cell(0,5,'Picking List',0,1,'C');
$pdf->Ln(15);
/* Order Number */
$pdf->SetFont('Helvetica','B',14);
$pdf->Cell(40,5,'Order Number: ');
$pdf->SetFont('Helvetica','',14);
$pdf->Cell(40,5,$result->increment_id);
$pdf->Ln(9);
/* Order Creation Date */
$pdf->SetFont('Helvetica','B',14);
$pdf->Cell(40,5,'Order Created: ');
$pdf->SetFont('Helvetica','',14);
$pdf->Cell(40,5,$result->created_at);
$pdf->Ln(9);
/* Shipping Name */
$pdf->SetFont('Helvetica','B',14);
$pdf->Cell(42,5,'Shipping Name: ');
$pdf->SetFont('Helvetica','',14);
$pdf->Cell(42,5,$result->shipping_address->firstname . ' ' . $result->shipping_address->lastname);
$pdf->Ln(9);
/* Order Items */
$pdf->SetFont('Helvetica','B',14);
$pdf->Cell(32,5,'Order Items: ');
$pdf->SetFont('Helvetica','',14);
$pdf->Cell(32,5,count($products));
$pdf->Ln(9);
/* Order Weight */
$pdf->SetFont('Helvetica','B',14);
$pdf->Cell(35,5,'Order Weight: ');
$pdf->SetFont('Helvetica','',14);
$pdf->Cell(35,5,$result->weight + 0);
$pdf->Ln(15);
/* Order Items */
$pdf->SetFont('Helvetica','B',12);
$pdf->Cell(17,8,' Item #',1);
$pdf->Cell(22,8,' Line Qty',1);
$pdf->Cell(29,8,' Line Weight',1);
$pdf->Cell(48,8,' Product SKU',1);
$pdf->Cell(75,8,' Product Name',1);
$pdf->SetFont('Helvetica','',11);
$pdf->Ln(8);
for ($i = 0, $c = count($products); $i < $c; $i++) {
$sku_string_width = $pdf->GetStringWidth($products[$i]->sku);
$name_string_width = $pdf->GetStringWidth($products[$i]->name);
$sku_column_width = 48;
$name_column_width = 75;
$sku_number_of_lines = $sku_string_width / ($sku_column_width - 1);
$name_number_of_lines = $name_string_width / ($name_column_width - 1);
$number_of_lines = ceil(max($sku_number_of_lines, $name_number_of_lines));
$standard_height_of_cells = 7;
$height_of_cells = $number_of_lines * $standard_height_of_cells;
$height_of_cells = ceil($height_of_cells);
/* Item Number */
$pdf->Cell(17,$height_of_cells,$i + 1,1);
/* Line Quantity */
$pdf->Cell(22,$height_of_cells,$products[$i]->qty_ordered + 0,1);
/* Line Weight */
$pdf->Cell(29,$height_of_cells,$products[$i]->row_weight + 0,1);
/* Product SKU */
$current_y = $pdf->GetY();
$current_x = $pdf->GetX();
$pdf->MultiCell($sku_column_width,$standard_height_of_cells,$products[$i]->sku,1);
$end_y = $pdf->GetY();
/* Product Name */
$current_x = $current_x + $sku_column_width;
$pdf->SetXY($current_x, $current_y);
$pdf->MultiCell($name_column_width,$standard_height_of_cells,$products[$i]->name,1);
$end_y = ($pdf->GetY() > $end_y)?$pdf->GetY() : $end_y;
}
/* Output PDF - "Picking List" */
$pdf->Output('D',$order_number . '_picking_list.pdf');
} catch (SoapFault $e) {
echo 'Error: ', $e->getMessage(), '<hr>';
}
?>