Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 784173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:40:14+00:00 2026-05-14T20:40:14+00:00

I am generating a PDF with fPDF. I need to strikethrough a long text

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:40:15+00:00Added an answer on May 14, 2026 at 8:40 pm

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am facing a problem with FPDF while generating the PDF file with images
I'm working with generating .pdf's from PHP using this library: http://www.fpdf.org/ I am currently
I am generating pdf invoice using fpdf. Some invoices containing many items, and details
For generating PDF from HTML, i need to fill a variable with output from
I'm dynamically generating a PDF using ABCpdf which contains a table of contents that
I am using com.lowagie.text.FontFactory in generating a PDF file and am trying to use
I am using the FPDF library for generating PDF files by PHP. This library
I am generating PDF/HTML report from a BIRT template. I am using visibility property
I'm generating pdf using ReportLab. But when i'm trying to attach image in it,
I am writing some XML -> PDF generating templates in Apache FOP for an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.