I'm using the function GetStringWidth()
to calculate how many lines I need for a MultiCell()
.
The column width is 75
and the result of GetStringWidth()
is 212
, therefore 3 lines are required for my text to fit into my MultiCell()
adequately.
The issue is that the output of this string when using MultiCell()
is spanning over 4 lines. I then realised that this is probably because the word spacing is oversized for this string within the output, but there is no reason that this should be happening.
See the word spacing below when the string is output using MultiCell()
(you can see the normal word spacing in the headers above):
This is the output when I tested with the same string using the Cell()
function (ignore the formatting). The difference is noticeable:
It looks to be that either GetStringWidth()
doesn't compare correctly with MultiCell()
or that MultiCell()
has a larger word spacing than Cell()
.
I've tried with multiple fonts so I can confirm that this isn't a font specific issue.
Can anybody help me identify why this could possible be happening? My code is below. Thank you.
<?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>';
}
?>