Jump to content

Wordwrap issue with FPDF


bruckerrlb

Recommended Posts

Not sure if this is the right place to post this, but I had a question regarding FPDF. Basically, I have been using the only thing I can find to wrap text using FPDF, which is a plugin here [link]http://www.fpdf.de/downloads/addons/49/[/link]

 

It's pretty self explanatory, and I'm testing it out and working with it and what not, basically to print, I have the following code:

 

$test1=str_repeat('test1 will print out 60 times ', 60);
//pass it through the wordwrap function
$nf=$pdf->WordWrap($test1, 50);
$pdf->Write(5, "This paragraph has $nf lines:\n\n");
//the following writes out $test1 all nicely with the word wrap
//$pdf->Write(5, $test1);
//The next line was my attempt to place this text wherever I want, but it doesn't seem to work
$test2=$pdf->Write(5, $test1);
$pdf->Text(80,179,"$test2");


 

From this code, you can see the problem is I'm trying to get the word wrapped text to print out at my coordinates, but it just ignores the wordwrap part and prints the text in one huge line at the coordinates instead of wrapping the text at my coordinates.

 

The code to make this work is here

<?php
define('FPDF_FONTPATH', 'font/');
require('includes/fpdf/fpdf.php');

class PDF extends FPDF
{
function WordWrap(&$rim, $maxwidth)
{
    $rim = trim($rim);
    if ($rim==='')
        return 0;
    $space = $this->GetStringWidth(' ');
    $lines = explode("\n", $rim);
    $rim = '';
    $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;
                $rim .= $word.' ';
            }
            else
            {
                $width = $wordwidth + $space;
                $rim = rtrim($rim)."\n".$word.' ';
                $count++;
            }
        }
        $rim = rtrim($rim)."\n";
        $count++;
    }
    $rim = rtrim($rim);
    return $count;
}
}

 

Has anyone used wordwrap with fpdf? Can I use the

 

$pdf->Text 
//or does it have to be like this
$pdf->Write

Link to comment
https://forums.phpfreaks.com/topic/191691-wordwrap-issue-with-fpdf/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.