I’ve code like that below, what I want to achieve is to generate PDF document with dynamically created rows. It’s for users to generate PDF from a form they fill. If they want to put some more text in one of the fields it should have been splitted into rows.
Idea is like that: explode text, and add each word with space after checking if size of it is less than 70 (required chars in a row). Unfortunately there is something wrong with the WHILE loop, causing my script not to generate any PDF file. Can anyone help? I can’t see what I’m doing wrong here.
//length of line is 70 chars
$n = strlen($datan['reason']) / 70;
//if there are some chars left add last line
if(strlen($datan['reason'])%70 > 0)
{
$n++;
}
$pieces = explode(" ",$datan['reason']);
$piece_number = 0;
//create n lines
for($i=0;$i<$n;$i++){
$previous = "";
$l = true;
//add pieces not to exceed 70 chars
while($l == true){
$current = $pieces[$piece_number];
if(strlen($current) + strlen($previous) < 70){
$previous .= $current . " ";
$piece_number++;
}
else
$l = false;
}
//print line
$pdf->addText(215,535+(20*$i),10,$previous);
}
ok, problem was in the while as I expected, it couldn’t end because it ran out of $pieces array and I forgot to control that.