fPDF:如何在多单元格中删除/删除对齐的文本?

发布于 2024-09-01 08:54:56 字数 1351 浏览 2 评论 0原文

我正在使用 fPDF 生成 PDF。

我需要删除 MultiCell 中的长文本。文本左右对齐,这可能是问题的根源。

这是我的代码:

//get the starting x and y of the cell to strikeout
$strikeout_y_start = $pdf->GetY();
$strikeout_x = $pdf->getX();
$strikeText = "Some text with no New Lines (\n), which is wrapped automaticly, cause it is  very very very very very very very very very very long long long long long long long long long long long long long long long long long long"
//draw the text
$pdf->MultiCell(180, 4, $strikeText);
//get the y end of cell
$strikeout_y_end = $pdf->GetY();
$strikeout_y = $strikeout_y_start+2;
$strikeCount = 0;
for ($strikeout_y; $strikeout_y < $strikeout_y_end - 4; $strikeout_y+=4) {
    $strikeCount++;
    //strike out the full width of all lines but last one - works OK
    $pdf->Line($strikeout_x, $strikeout_y, $strikeout_x + 180, $strikeout_y);
}

//this works, but gives incorrect results
$width = $pdf->GetStringWidth($strikeText);
$width = $width - $strikeCount*180;
//the line below will strike out some text, but not all the letters of last line
$pdf->line($strikeout_x, $strikeout_y, $strikeout_x+$width, $strikeout_y);

问题是,由于多单元格中的文本是合理的(并且必须如此),前几行中的 spacec 比 GetStringWidth 假设的宽,因此 GetStringWidth 低估了该文本的完整宽度。

结果,最后一行被描边了 70%,并且其末尾的一些字母没有被描边。

有什么想法如何计算多单元格中最后一行的宽度吗?

I am generating a PDF with fPDF.

I need to strikethrough a long text inside a MultiCell. The text is justified to left and right, which probably is the source of the problem.

Here is my code:

//get the starting x and y of the cell to strikeout
$strikeout_y_start = $pdf->GetY();
$strikeout_x = $pdf->getX();
$strikeText = "Some text with no New Lines (\n), which is wrapped automaticly, cause it is  very very very very very very very very very very long long long long long long long long long long long long long long long long long long"
//draw the text
$pdf->MultiCell(180, 4, $strikeText);
//get the y end of cell
$strikeout_y_end = $pdf->GetY();
$strikeout_y = $strikeout_y_start+2;
$strikeCount = 0;
for ($strikeout_y; $strikeout_y < $strikeout_y_end - 4; $strikeout_y+=4) {
    $strikeCount++;
    //strike out the full width of all lines but last one - works OK
    $pdf->Line($strikeout_x, $strikeout_y, $strikeout_x + 180, $strikeout_y);
}

//this works, but gives incorrect results
$width = $pdf->GetStringWidth($strikeText);
$width = $width - $strikeCount*180;
//the line below will strike out some text, but not all the letters of last line
$pdf->line($strikeout_x, $strikeout_y, $strikeout_x+$width, $strikeout_y);

The problem is that as the text in multicell is justified (and have to be), the spacec in previous lines are wider than the GetStringWidth assumes, so GetStringWidth underestimates the full width of this text.

As a result, the last line is stroked out in, say, 70%, and some letters on the end of it are not stroked out.

Any ideas how to calculate the width of last line in multicell?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妥活 2024-09-08 08:54:56

我自己找到了解决方案。
很抱歉提出不必要的问题。

这是我所做的:

class VeraPDF extends FPDF {

    /**
     * Returns width of the last line in a multicell
     * useful for strike out / strike through 
     * 
     *
     * @param string $s - the string measured
     * @param int $lineWidth - with of the cell/line
     * @return int
     */
    function GetStringWidth_JustifiedLastLineWidth($s, $lineWidth)
    {
        //Get width of a string in the current font
        $s=(string)$s;
        $words = split(' ',$s);
        $cw=&$this->CurrentFont['cw'];
        $w=0;
        $spaceWidth = $this->GetStringWidth(' ');

        for($i=0, $wordsCount = count($words); $i<$wordsCount; $i++){
            // sum up all the words width, and add space withs between the words
            $w += $this->GetStringWidth($words[$i]) + $spaceWidth;
            if ($w > $lineWidth) {
                //if current width is more than the line width, then the current word
                //will be moved to next line, we need to count it again
                $i--;
            }
            if ($w >= $lineWidth) {
                //if the current width is equal or grater than the line width, 
                //we need to reset current width, and count the width of remaining text
                $w = 0;
            }
        }
        //at last, we have only the width of the text that remain on the last line!
        return $w;
    }    
}

希望这对某人有帮助:)

I found the solution myself.
Sorry for asking unnecessary questions.

Here is what I had done:

class VeraPDF extends FPDF {

    /**
     * Returns width of the last line in a multicell
     * useful for strike out / strike through 
     * 
     *
     * @param string $s - the string measured
     * @param int $lineWidth - with of the cell/line
     * @return int
     */
    function GetStringWidth_JustifiedLastLineWidth($s, $lineWidth)
    {
        //Get width of a string in the current font
        $s=(string)$s;
        $words = split(' ',$s);
        $cw=&$this->CurrentFont['cw'];
        $w=0;
        $spaceWidth = $this->GetStringWidth(' ');

        for($i=0, $wordsCount = count($words); $i<$wordsCount; $i++){
            // sum up all the words width, and add space withs between the words
            $w += $this->GetStringWidth($words[$i]) + $spaceWidth;
            if ($w > $lineWidth) {
                //if current width is more than the line width, then the current word
                //will be moved to next line, we need to count it again
                $i--;
            }
            if ($w >= $lineWidth) {
                //if the current width is equal or grater than the line width, 
                //we need to reset current width, and count the width of remaining text
                $w = 0;
            }
        }
        //at last, we have only the width of the text that remain on the last line!
        return $w;
    }    
}

Hope this helped someone :)

别闹i 2024-09-08 08:54:56

前面几行的 spacec 更宽
比 GetStringWidth 假设的要大,所以
GetStringWidth 低估了完整的
该文本的宽度。

您是否尝试过计算空格并自己添加缺少的宽度。假设每个空间应该是 5px 宽,但 fpdf 估计它是 4px,也许你可以在最后为每个空间添加 1px 到总宽度。

the spacec in previous lines are wider
than the GetStringWidth assumes, so
GetStringWidth underestimates the full
width of this text.

Have you tried to count the spaces and add the missing width yourself. Say every space is supposed to be 5px wide, but fpdf etimates it to be 4px, maybe you could add 1px per space to the total width in the end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文