多单元元素的FPDF高度 [英] FPDF height of a MultiCell Element

查看:59
本文介绍了多单元元素的FPDF高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用FPDF库将某些文档文件导出为PDF.一个文档包含一串长度不同的字符串.我将所有字符串打印为$pdf->MultiCell().现在,我想让该MultiCell的当前高度具有相同的行距,以防它们只有一行或更多行.

I use the FPDF library to export some document files as PDF. One document includes a list of strings which have a different length. I print all strings as $pdf->MultiCell(). Now I would like to have the current height of that MultiCell to have the same line spacing in case that they have just one line or more.

代码示例:

//MySQL Query
while($row = mysql_fetch_array($res) {
   $pdf->SetXY(18, $x);
   $pdf->MultiCell(80, 5, $rowr['text']); //text has one or more lines
   $x = $x + 10; // Here I would prefer a solution to say: $x = $x + 2 + height of the MultiCell()
}

推荐答案

我遇到了完全相同的问题;我使用FPDF生成发票,每行有四个单元格,第一个单元格是高度可变的MultiCell(默认高度为5,但是如果订单名称太长,则会在总高度10上添加另一行,依此类推) .问题是其余3个单元格的高度固定.

I had the exact same problem; I use FPDF to generate invoices and there are four cells per row with first cell being a MultiCell with varying height (default height is 5, but if order name is too long it adds another line to a total height of 10, and so on). The problem is that the remaining 3 cells have fixed height.

寻找解决方案后,看来唯一的方法是编写复杂的函数或使用某些第三方工具.因为我希望我的应用程序尽可能轻巧,所以我使用以下方法来解决它,我认为这比外部插件要简单得多.

After looking for solutions it seems the only way is to write a complex function or use some third party tool. Since I want my app to be as light as possible I used the following approach to solve it which in my opinion is way simpler than external plugin.

  1. 包含发票明细的行从Y = 95开始,所以我使用$Y=95; 在我的while循环之前
  2. 我的第一个单元格(MultiCell)如下:

  1. Rows with details on the Invoice start at Y=95 so I use $Y=95; before my while loop
  2. My first cell (the MultiCell) is as follows:

$pdf->SetXY(10,$Y);
$pdf->MultiCell(60,5,$row['Name'],1,1,'L');

  • 我使用FPDF的GetY()函数获取当前高度并将其保存为H:

  • I use the FPDF's GetY() function to get current height and save it as H:

    $H = $pdf->GetY();
    

    如果MultiCell的高度为5,则GetY()将返回100,如果MultiCell的高度为10,则GetY()将返回105,依此类推.

    If the MultiCell's height is 5 GetY() will give back 100, if the height is 10 GetY() gives back 105 and so on.

    我添加了新变量$ height:

    I add new variable $height:

    $height= $H-$Y;
    

    结果使我准确地知道了MultiCell的高度.

    Which as result gives me precisely the height of the MultiCell.

    我使用$ Y和$ height设置当前位置和列高:

    I use $Y and $height to set current position and column height:

    $pdf->SetXY(130,$Y);
    $pdf->Cell(40,$height,$row['RowName'],1,1,'L');
    

  • 在完成while循环设置之前,请为$ Y提供$ H的值:

  • Before finishing the while loop set give $Y the $H's value:

    $Y=$H;
    

  • 整个循环如下所示,并且效果很好:

    Entire loop looks as follows and works perfectly:

    $Y= 95;
    
    $query = mysqli_query($con,"SELECT * FROM table");
    
    while($row = mysqli_fetch_array($query)) {
        $pdf->SetXY(10,$Y);
        $pdf->MultiCell(60,5,$row['ROW1'],1,1,'L');
    
        $H = $pdf->GetY();
        $height= $H-$Y;
    
        $pdf->SetXY(70,$Y);
        $pdf->Cell(60,$height,$row['ROW2'],1,1,'L');
        $pdf->SetXY(130,$Y);
        $pdf->Cell(40,$height,$row['ROW3'],1,1,'L');
        $pdf->SetXY(170,$Y);
        $pdf->Cell(30,$height,$row['ROW4'],1,1,'L');
    
        $Y=$H;
    }
    

    如果每行中有2个或更多的MultiCell列,这会很棘手,但仍然可以用类似的方式解决.

    If you have 2 MultiCell columns or more in each row it gets tricky but still can be solved in a similar manner.

    这篇关于多单元元素的FPDF高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆