php FPDF 中 MultiCell 的换行问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1419719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
line break problem with MultiCell in FPDF
提问by user156073
I am using java port of fpdf. I am encountering fowwlowing errors.
我正在使用 fpdf 的 java 端口。我遇到了流动错误。
1).When i call multicell 2 times every time the text is printed on a new line.
1).当我每次在新行上打印文本时调用 multicell 2 次。
MultiCell(0, 1, "abcd", currentBorders, Alignment.LEFT, false); //prints on one line
MultiCell(0, 1, "efg", currentBorders, Alignment.LEFT, false); //prints on next line
I want that there is no line break after the call to multicell. How can i do it?
我希望在调用 multicell 后没有换行符。我该怎么做?
2)If i do the following thing then some part of my string gets printed on one line and some on next.
2)如果我执行以下操作,那么我的字符串的一部分会打印在一行上,而另一部分会打印在下一行。
MultiCell(getStringWidth(myString), 1, myStringcurrentBorders, Alignment.LEFT, false);
3)If i do the following thing then there are many blank lines after the line on which myString is printed. It works correctly if i use one 1 ans second parameter
3)如果我执行以下操作,则在打印 myString 的行之后有许多空行。如果我使用一个 1 ans 第二个参数,它可以正常工作
MultiCell(0, myFontSize, "123456", currentBorders, Alignment.LEFT, false);
What is the problem?
问题是什么?
回答by Joshua Pinter
I would get the current Yposition before writing the MultiCelland then move the "cursor" back to that Yposition after the MultiCellgeneration. Like this:
我会Y在写入之前获取当前位置MultiCell,然后Y在MultiCell生成之后将“光标”移回该位置。像这样:
$current_y = $pdf->GetY();
$current_x = $pdf->GetX();
$cell_width = 50;
MultiCell($cell_width, 1, "abcd", currentBorders, Alignment.LEFT, false);
$pdf->SetXY($current_x + $cell_width, $current_y);
$current_x = $pdf->GetX();
MultiCell($cell_width, 1, "abcd", currentBorders, Alignment.LEFT, false);
Something like that.
类似的东西。
回答by Muhammad Abdul-Rahim
I created a new method called MultiAlignCell. It takes the same parameters as MultiCellbut with the added lnfield from Cell. You can add it to your extended FPDFclass.
我创建了一个名为MultiAlignCell. 它采用相同的参数,MultiCell但添加了ln来自 的字段Cell。您可以将其添加到您的扩展FPDF类中。
/**
* MultiCell with alignment as in Cell.
* @param float $w
* @param float $h
* @param string $text
* @param mixed $border
* @param int $ln
* @param string $align
* @param boolean $fill
*/
private function MultiAlignCell($w,$h,$text,$border=0,$ln=0,$align='L',$fill=false)
{
// Store reset values for (x,y) positions
$x = $this->GetX() + $w;
$y = $this->GetY();
// Make a call to FPDF's MultiCell
$this->MultiCell($w,$h,$text,$border,$align,$fill);
// Reset the line position to the right, like in Cell
if( $ln==0 )
{
$this->SetXY($x,$y);
}
}
回答by tomazahlin
I have modified the MultiCell method, it works as the above answer, and you can use the method in the same way as the Cell method.
我已经修改了 MultiCell 方法,它的工作原理与上面的答案相同,您可以按照与 Cell 方法相同的方式使用该方法。
function MultiCell($w, $h, $txt, $border=0, $ln=0, $align='J', $fill=false)
{
// Custom Tomaz Ahlin
if($ln == 0) {
$current_y = $this->GetY();
$current_x = $this->GetX();
}
// Output text with automatic or explicit line breaks
$cw = &$this->CurrentFont['cw'];
if($w==0)
$w = $this->w-$this->rMargin-$this->x;
$wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
$s = str_replace("\r",'',$txt);
$nb = strlen($s);
if($nb>0 && $s[$nb-1]=="\n")
$nb--;
$b = 0;
if($border)
{
if($border==1)
{
$border = 'LTRB';
$b = 'LRT';
$b2 = 'LR';
}
else
{
$b2 = '';
if(strpos($border,'L')!==false)
$b2 .= 'L';
if(strpos($border,'R')!==false)
$b2 .= 'R';
$b = (strpos($border,'T')!==false) ? $b2.'T' : $b2;
}
}
$sep = -1;
$i = 0;
$j = 0;
$l = 0;
$ns = 0;
$nl = 1;
while($i<$nb)
{
// Get next character
$c = $s[$i];
if($c=="\n")
{
// Explicit line break
if($this->ws>0)
{
$this->ws = 0;
$this->_out('0 Tw');
}
$this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
$i++;
$sep = -1;
$j = $i;
$l = 0;
$ns = 0;
$nl++;
if($border && $nl==2)
$b = $b2;
continue;
}
if($c==' ')
{
$sep = $i;
$ls = $l;
$ns++;
}
$l += $cw[$c];
if($l>$wmax)
{
// Automatic line break
if($sep==-1)
{
if($i==$j)
$i++;
if($this->ws>0)
{
$this->ws = 0;
$this->_out('0 Tw');
}
$this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
}
else
{
if($align=='J')
{
$this->ws = ($ns>1) ? ($wmax-$ls)/1000*$this->FontSize/($ns-1) : 0;
$this->_out(sprintf('%.3F Tw',$this->ws*$this->k));
}
$this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
$i = $sep+1;
}
$sep = -1;
$j = $i;
$l = 0;
$ns = 0;
$nl++;
if($border && $nl==2)
$b = $b2;
}
else
$i++;
}
// Last chunk
if($this->ws>0)
{
$this->ws = 0;
$this->_out('0 Tw');
}
if($border && strpos($border,'B')!==false)
$b .= 'B';
$this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
$this->x = $this->lMargin;
// Custom Tomaz Ahlin
if($ln == 0) {
$this->SetXY($current_x + $w, $current_y);
}
}

