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

### 问题描述

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

## 推荐答案

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;
}
```

## 其他推荐答案

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

## 其他推荐答案

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

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

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

### 问题描述

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->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.