TCPDF:将文本剪切为单元格宽度

编程入门 行业动态 更新时间:2024-10-11 11:20:58
本文介绍了TCPDF:将文本剪切为单元格宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在广泛使用TCPDF的Cell方法生成PDF报告。使用Cell方法打印的文本溢出超出方法指定的宽度。我只想打印适合指定宽度的大部分文本,但不希望溢出到下一行。我不想字体扩展策略。

I am generating PDF report using TCPDF's Cell method extensively. Text printed with Cell method spills beyond width specified in method. I want to print only as much part of the text that fits in the specified width but not to spill beyond or wrap to next line. I do not want font stretch strategy.

我搜索了很多,但是找不到解决方案。还有其他方法/方式来处理吗? (我使用setfillcolor(255)来实现视觉效果。但是文本仍然在那里,不可见;当您尝试选择时会显示出来。)

I searched a lot but could not find a solution. Is there any other method/way to handle this? (I used setfillcolor(255) to achieve the visual effect. But the text is still there, invisible; gets revealed when you try to select.)

$pdf->SetFillColor(255); // only visual effect $pdf->Cell(36, 0, "A very big text in the first column, getting printed in 3.6cm width", 0, 0, 'L', true); $pdf->Cell(20, 0, "Data 1", 0, 0, 'L', true); $pdf->Cell(20, 0, "Data 2", 0, 0, 'L', true);

非常感谢。

推荐答案

我在此处找到答案由TCPDF的主要作者Nicola Asuni撰写。用户 fenstra 提供的以下代码对我有用:

I have found an answer here by Nicola Asuni, who is the main TCPDF author. The following code, provided by user fenstra, is working for me:

// Start clipping. $pdf->StartTransform(); // Draw clipping rectangle to match html cell. $pdf->Rect($x, $y, $w, $h, 'CNZ'); // Output html. $pdf->writeHTMLCell($w, $h, $x, $y, $html); // Stop clipping. $pdf->StopTransform();

据我所知,剪切矩形不会考虑显示文本的任何填充,因此,如果需要模仿MultiCell在此特定对象上的行为,可以对Rect的宽度和高度应用适当的数学运算。

As far as I can tell, the clipping rectangle won't consider any padding on the displayed text, so you apply the proper math to Rect's width and height if you need to mimic the behaviour of a MultiCell on this particular.

更多推荐

TCPDF:将文本剪切为单元格宽度

本文发布于:2023-11-23 01:08:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619563.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:宽度   单元格   文本   TCPDF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!