2017-07-26 31 views
0

我试图用FPDF在PDF文档上显示一些数据,我的问题是我无法限制字符串的字符数,有时宽度超过了,我已经使用MultiCell,但我想设置一个字符的限制,使用FPDF限制字符串中的字符数

我试图用我的函数自定义回声来解决这个问题,但显然不能使用fpdf我不知道会发生什么。

function custom_echo($x, $length) 
{ 
    if(strlen($x)<=$length) 
    { 
     echo $x; 
    } 
    else 
    { 
     $y=substr($x,0,$length) . '...'; 
     echo $y; 
    } 

} 

$message= "HELLO WORLD"; 

$pdf=new FPDF(); 
$pdf->SetLeftMargin(0); 
$pdf->AddPage(); 

$pdf->MultiCell(95, 6, utf8_decode(custom_echo($message,5)), 0, 1); 
// already tried this 
$pdf->MultiCell(95, 6, custom_echo(utf8_decode($message),5), 0, 1); 

$pdf->Output(); 

回答

0

的PHP echo命令发送一个字符串输出。您需要返回字符串作为函数的结果,以便FPDF可以使用它。

function custom_echo($x, $length) { 
    if (strlen($x) <= $length) { 
     return $x; 
    } else { 
     return substr($x,0,$length) . '...'; 
    } 
} 

这可以简化为:

function custom_echo($x, $length) { 
    if (strlen($x) <= $length) { 
     return $x; 
    } 
    return substr($x,0,$length) . '...'; 
} 

,并可以作出更短,但是这是我会怎么做。

+0

我不能相信那个错误,谢谢你的时间! –

+0

@edgarreyes我也犯过这样的错误。 – manassehkatz