多细胞元件的FPDF高度[英] FPDF height of a MultiCell Element

本文是小编为大家收集整理的关于多细胞元件的FPDF高度的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我使用FPDF库将一些文档文件作为PDF导出.一个文档包括一个具有不同长度的字符串列表.我将所有字符串打印为$pdf->MultiCell().现在,我想拥有当前的多电池的高度,以便在只有一条或更长时间的情况下具有相同的线间距.

代码示例:

//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生成发票,每行有四个单元格,第一个单元格是一个具有不同高度的多电池(默认高度为5,但是如果订单名称太长,则将另一行添加到总高度10,等等) .问题是其余3个单元具有固定高度.

在寻找解决方案后,似乎唯一的方法是编写复杂的功能或使用某些第三方工具.由于我希望我的应用尽可能轻巧,因此我使用以下方法来解决它,这比外部插件更简单.

  1. 行,发票上的详细信息从y = 95开始,所以我使用$Y=95; 在我段循环之前
  2. 我的第一个单元格(多切尔)如下:

    $pdf->SetXY(10,$Y);
    $pdf->MultiCell(60,5,$row['Name'],1,1,'L');
    
  3. 我使用fpdf的gety()函数获取当前高度并将其保存为H:

    $H = $pdf->GetY();
    

    如果多电池的高度为5 gety(),如果高度为10 gety()回馈105,等等.

  4. .
  5. 我添加了新变量$高度:

    $height= $H-$Y;
    

    结果使我完全赋予了多电池的高度.

  6. 我使用$ y和$高度来设置当前位置和列高:

    $pdf->SetXY(130,$Y);
    $pdf->Cell(40,$height,$row['RowName'],1,1,'L');
    
  7. 在完成WALE循环集之前,请给出$ y的$ H值:

    $Y=$H;
    

整个循环看起来如下,正常工作:

$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列或更多列,则很棘手,但仍然可以以类似的方式解决.

其他推荐答案

我在这里找到了有趣的解决方案-e https://github.com/artkonekt/pdf-invoice/blob/master/src/invoiceprinter.php#l469

$calculateHeight = new self;
$calculateHeight->addPage();
$calculateHeight->setXY(0, 0);
$calculateHeight->SetFont($this->font, '', 7);
$calculateHeight->MultiCell($this->firstColumnWidth, 3, $item['description'], 0, 'L', 1);
$descriptionHeight = $calculateHeight->getY() + $cellHeight + 2;

因此,他从字面上创建了一个"临时" PDF,添加多电池,然后简单地测量高度(Newy -oldy)

另外,请记住,如果文本转到新行 - 单元格的高度将为= number_of_lines * $height(高度传递给Multicell作为第二个参数)

因此,如果您通过5通过$ height,并且临时PDF测量该单元将为15,则可以确定文本将扩散到3行.

其他推荐答案

我正在Golang编码,所以我会显示一些伪代码.我希望php中的可访问方法与golang中的方法相同.

有一种称为pdf.SplitLines(text, width)的方法.您将传递字符串内容和所需的宽度,它将返回一系列字符串,代表将计算出来显示该内容的行.

很容易.在伪代码中,它看起来像:

fontSize = 10;
lineHeight = 12;
targetWidth = 50;
pdf.SetFontSize(fontSize)
nLines = length(pdf.SplitLines(content, targetWidth));

multiCellHeight = nLines * lineHeight;
pdf.Multicell(targetWidth, lineHeight, content, ...) 

渲染MultiCell的大小将与multiCellHeight中存储的大小完全相同.这样,您将在渲染之前获得高峰.

这是因为MultiCell传递的height是每一行的php.如果您在渲染前知道行,您将获得总高度.


,如果出于任何原因失败了php,我很抱歉.只是让我知道是否就是这样.

本文地址:https://www.itbaoku.cn/post/2090895.html

问题描述

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.

Code Example:

//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()
}

推荐答案

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. 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');
    
  3. I use the FPDF's GetY() function to get current height and save it as H:

    $H = $pdf->GetY();
    

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

  4. I add new variable $height:

    $height= $H-$Y;
    

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

  5. 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');
    
  6. 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;
}

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

其他推荐答案

I found interesting solution here - https://github.com/artkonekt/pdf-invoice/blob/master/src/InvoicePrinter.php#L469

$calculateHeight = new self;
$calculateHeight->addPage();
$calculateHeight->setXY(0, 0);
$calculateHeight->SetFont($this->font, '', 7);
$calculateHeight->MultiCell($this->firstColumnWidth, 3, $item['description'], 0, 'L', 1);
$descriptionHeight = $calculateHeight->getY() + $cellHeight + 2;

So, he literally create a 'temporary' PDF, add multicell, and then simply measure height (newY - oldY)

Also, keep in mind that if text goes to new line - height of cell will be = number_of_lines * $height (height passed to MultiCell as second parameter)

So, if you passed 5 as $height, and temporary PDF measure that cell will be 15, you can be sure that text will spread to 3 lines.

其他推荐答案

I'm coding in golang so I'll show some pseudo-code. I hope the accessible methods are the same in php as in golang.

There is a method called pdf.SplitLines(text, width). You will pass your string content and the desired width and it will return an array of strings that represents the lines that'll be computed to display that content.

With that its easy. In pseudo-code it could look like:

fontSize = 10;
lineHeight = 12;
targetWidth = 50;
pdf.SetFontSize(fontSize)
nLines = length(pdf.SplitLines(content, targetWidth));

multiCellHeight = nLines * lineHeight;
pdf.Multicell(targetWidth, lineHeight, content, ...) 

The rendered MultiCell will have the exact same size as stored in multiCellHeight. This way you'll get the hight before rendering.

This is working because the passed height for the MultiCell is the lineHeight of each row. If you know the rows before rendering, you'll get the total height.


I'm sorry if this fails for any reason for php. Just let me know if that's the case.