Word wrap

Informations

Author: Ron Korving
License: FPDF

Description

If you want to wrap some text without rendering it, you can use this simple function.

int WordWrap(string &text, float maxwidth)

It returns the total number of lines that the string consists of. The text parameter is overwritten (call by reference) with the new wrapped text. The maxwidth parameter is the maximum width a line may be, given in the unit set previously in your FPDF object.

Source

<?php
require('fpdf.php');

class PDF extends FPDF
{
function WordWrap(&$text, $maxwidth)
{
    $text = trim($text);
    if ($text==='')
        return 0;
    $space = $this->GetStringWidth(' ');
    $lines = explode("\n", $text);
    $text = '';
    $count = 0;

    foreach ($lines as $line)
    {
        $words = preg_split('/ +/', $line);
        $width = 0;

        foreach ($words as $word)
        {
            $wordwidth = $this->GetStringWidth($word);
            if ($wordwidth > $maxwidth)
            {
                // Word is too long, we cut it
                for($i=0; $i<strlen($word); $i++)
                {
                    $wordwidth = $this->GetStringWidth(substr($word, $i, 1));
                    if($width + $wordwidth <= $maxwidth)
                    {
                        $width += $wordwidth;
                        $text .= substr($word, $i, 1);
                    }
                    else
                    {
                        $width = $wordwidth;
                        $text = rtrim($text)."\n".substr($word, $i, 1);
                        $count++;
                    }
                }
            }
            elseif($width + $wordwidth <= $maxwidth)
            {
                $width += $wordwidth + $space;
                $text .= $word.' ';
            }
            else
            {
                $width = $wordwidth + $space;
                $text = rtrim($text)."\n".$word.' ';
                $count++;
            }
        }
        $text = rtrim($text)."\n";
        $count++;
    }
    $text = rtrim($text);
    return $count;
}
}
?>

Example

<?php
require('wordwrap.php');

$pdf=new PDF();
$pdf->AddPage();
$pdf->SetFont('Arial','',12);
$text=str_repeat('this is a word wrap test ',20);
$nb=$pdf->WordWrap($text,120);
$pdf->Write(5,"This paragraph has $nb lines:\n\n");
$pdf->Write(5,$text);
$pdf->Output();
?>
View the result here.

Download

ZIP | TGZ