PHP - Wrap text in a cell when using FPDF

by
Jeremy Canfield |
Updated: March 11 2020
| PHP articles
Create a class to extend FPDF with the vcell function.
class WrapText extends FPDF
{
function vcell($c_width,$c_height,$x_axis,$text){
$wrap=$c_height/24;
$wrap0=$wrap+16;// First line of text (+8 from previous)
$wrap1=$wrap+24;// Second line of text (+8 from previous)
$wrap2=$wrap+32;// Third line of text (+8 from previous)
$wrap3=$wrap+40;// Fourth line of text (+8 from previous)
$wrap4=$wrap+48;// Fifth line of text (+8 from previous)
$wrap5=$wrap+56;// Sixth line of text (+8 from previous)
$wrap6=$wrap+64;// Seventh line of text (+8 from previous)
$len=strlen($text);// Splits the text into 64 character each and saves in a array
if($len>64){
$wrap_text_array=str_split($text,64);//This sets the length of each array to 64 characters
$this->SetX($x_axis);
$this->Cell($c_width,$wrap0,$wrap_text_array[0],'','','');// First line of text
$this->SetX($x_axis);
$this->Cell($c_width,$wrap1,$wrap_text_array[1],'','','');// Second line of text
$this->SetX($x_axis);
$this->Cell($c_width,$wrap2,$wrap_text_array[2],'','','');// Third line of text
$this->SetX($x_axis);
$this->Cell($c_width,$wrap3,$wrap_text_array[3],'','','');// Fourth line of text
$this->SetX($x_axis);
$this->Cell($c_width,$wrap4,$wrap_text_array[4],'','','');// Fifth line of text
$this->SetX($x_axis);
$this->Cell($c_width,$wrap5,$wrap_text_array[5],'','','');// Sixth line of text
$this->SetX($x_axis);
$this->Cell($c_width,$wrap6,$wrap_text_array[6],'','','');// Seventh line of text
$this->SetX($x_axis);
$this->Cell($c_width,$c_height,'','LTR',0,'L',0);
}
else{
$this->SetX($x_axis);
$this->Cell($c_width,$c_height,$text,'LTRB',0,'L',0);}
}
}
Use the WrapText class.
$pdf = new WrapText();
Then use this to create the table row.
//line break
$pdf->Ln();
// width, heigth, x_axis, data, ?, border, alignment, enable background color
$c_width=115;
$c_height=35;
$text = $Example;
$pdf->Cell(40, 35, 'Example', 'TL' , 0, 'L', 0);
$pdf->Cell(40, 35, $Example, 'TL' , 0, 'L', 0);
$x_axis=$pdf->getx();
$pdf->vcell($c_width,$c_height,$x_axis,$text);
The inspiration for this script comes from this post on StackOverflow: http://stackoverflow.com/questions/23542244/wrap-text-in-fpdf-in-php.
Did you find this article helpful?
If so, consider buying me a coffee over at
Comments
December 31 2018 by Yasir
it gives an error !
Fatal error: Uncaught exception 'Exception' with message 'FPDF error: No font has been set' in E:\wamp64\www\darbar-sharif-inventory\admin-panel\fpdf\fpdf.php on line 271
July 21 2022 by Athira
it gives an error ! Fatal error: Uncaught exception 'Exception' with message 'FPDF error: No font has been set' in E:\wamp64\www\darbar-sharif-inventory\admin-panel\fpdf\fpdf.php on line 271
April 04 2023 by Rey Ary T
Can I get video tutorial how using this code, because I'm confused to using these codes, thank you :)