TCPDF: Clip text to Cell width

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 15:15:36

问题


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.

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

here is my part of code.

    $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);

Thanks a lot.


回答1:


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();

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.



来源:https://stackoverflow.com/questions/8163937/tcpdf-clip-text-to-cell-width

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!