0
我使用TCPDF来打印条码表单标签。 每个标签都有一个条形码和一些文字。 Evreything似乎工作正常,但有时文本是漫长的,'入侵'下一个标签/下一行。在TCPDF中使用GetStringWidth缩短字符串
我试图检查字符串的长度 - 并在需要短,如果:
$label_w = ($page_w-$right_mar-$left_mar)/$Col;
$text_width = $pdf->GetStringWidth($exploded_line[2]);
while ($text_width>$label_w-15) // "-15" because the text location
{
$exploded_line[2]=substr($exploded_line[2],0,-1);
$text_width = $pdf->GetStringWidth($exploded_line[2]);
}
是进入循环只是继续萎缩,直到只有首字母左的文字...
起初我以为问题是我的While
条件不是因为某种原因停止。 然后我试图将其更改为简单if
- 但问题是没有走......
if ($text_width>$label_w-15)
{
$exploded_line[2]=substr($exploded_line[2],0,-1);
$text_width = $pdf->GetStringWidth($exploded_line[2]);
}
有什么建议?谢谢。
您是否检查过'$ label_w'和'$ text_width'的值以确保它们是好的?逻辑看起来很好,基本上和我做的一样,但是我缩小了字体大小而不是删除字符。 – miken32
当然,'$ label_w'在这个例子中是70,'$ text_width'在40-62之间......如果它超过55(= 70-15),错误就会发生。 – HaReL