Hey all,
i’m having a problem with getting word wrap to work when i create a pdf dynamically from php…the text i wish to wrap is in a table. The table and cells doesn’t word wrap…so i was thinking maybe i could get a function and add it to the string? I have a string in my table cells. which has been seperated with 20 words each word is individual.
anyways here is the word wrap function that works with the pdf script:
<?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("
", $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)."
".substr($word, $i, 1);
$count++;
}
}
}
elseif($width + $wordwidth <= $maxwidth)
{
$width += $wordwidth + $space;
$text .= $word.' ';
}
else
{
$width = $wordwidth + $space;
$text = rtrim($text)."
".$word.' ';
$count++;
}
}
$text = rtrim($text)."
";
$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:
");
$pdf->Write(5,$text);
$pdf->Output();
?>
and here is my script so far:
<?php
if (!isset($words) || strlen($words)==0){
//$demotext='demo text';
$txt = explode( ',', $_COOKIE['cookie1'] );
$txt = implode( ',', $txt );
$string = $txt;
$chunks = spliti (",", $string, 20);
//print_r($chunks);
//$data = explode( ',', $data );
//$txt = array($txt);
//$words = unserialize( $_COOKIE['cookie1'] );
//print $_COOKIE['cookie1'];
//print $txt;
}
//require('fpdf.php');
require('html_table.php');
$html='<table border="0">
<tr>
<td width="190" height="125" align="center">'.$chunks[0].'</td><td width="190" height="125" align="center">'.$chunks[1].'</td><td width="190" height="125" align="center">'.$chunks[2].'</td><td width="190" height="125" align="center">'.$chunks[3].'</td>
</tr>
<tr>
<td width="190" height="125" align="center">'.$chunks[4].'</td><td width="190" height="125" align="center">'.$chunks[5].'</td><td width="190" height="125" align="center">'.$chunks[6].'</td><td width="190" height="125" align="center">'.$chunks[7].'</td>
</tr>
<tr>
<td width="190" height="125" align="center">'.$chunks[8].'</td><td width="190" height="125" align="center">'.$chunks[9].'</td><td width="190" height="125" align="center">'.$chunks[10].'</td><td width="190" height="125" align="center">'.$chunks[11].'</td>
</tr>
<tr>
<td width="190" height="125" align="center">'.$chunks[12].'</td><td width="190" height="125" align="center">'.$chunks[13].'</td><td width="190" height="125" align="center">'.$chunks[14].'</td><td width="190" height="125" align="center">'.$chunks[15].'</td>
</tr>
<tr>
<td width="190" height="125" align="center">'.$chunks[16].'</td><td width="190" height="125" align="center">'.$chunks[17].'</td><td width="190" height="125" align="center">'.$chunks[18].'</td><td width="190" height="125" align="center">'.$chunks[19].'</td>
</tr>
</table>';
$pdf=new PDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',10);
$pdf->Image('pdfbg.jpg',0.4,0.3,0,0);
$pdf->SetY(148);
$pdf->SetDisplayMode('fullpage','continuous');
$pdf->WriteHTML($html);
//$pdf->WriteParaHTML($html);
//$pdf->Text(40,160,$txt);
$pdf->Output();
//$pdf->setXY(20,-40);
//$pdf->SetFont('Arial','',7);
?>
I get the words from my cookie as you can see above.
So my problem is that i need each word to wrap ideally once it exceeds the cell…but if not i can set a default width for each string which is smaller than the cell so it will wrap before the end of the cell.
How can i implement these two things either into my scirpt i have so far or into the string???
Thanks in advance