fpdf
PHP powered
home was ist fpdf? downloads & add-ons dokumentation faq forum impressum links kontakt
 

Downloads > Add-On

 
         
 

Word wrap

Informations

Author: Ron Korving
License: Freeware

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
define
('FPDF_FONTPATH', 'font/');
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 ($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;
}
}

$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
 






 
         
         
 
 
fpdf.de © Copyright 2004-2006 carrib internet solutions - Beachten Sie bitte die Nutzungsbedingungen
Eine Verwendung der auf fpdf.de befindlichen Inhalte (Texte, Grafiken) ist nur mit Zustimmung des Betreibers zulässig.