dousikuai5417 2017-01-22 17:43 采纳率: 0%
浏览 267

FPDF - 使用MultiCell()的超大字间距问题

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):

enter image description here

This is the output when I tested with the same string using the Cell() function (ignore the formatting). The difference is noticeable:

enter image description here

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>';
    }
?>
  • 写回答

1条回答 默认 最新

  • 陆山寺 2021-09-25 00:59
    关注

    has anybuddy solved this issue?

    Hi has anybuddy solved this issue?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大