How to put breaks in string fetching from database to view on FPDF

1.5k views Asked by At

I am printing the contents of MySql table on PDF using FPDF. I have a column for text where usually long strings are input. When i am printing this on FPDF using Cell function the whole string is exceeding the cell it should be in. I want that whole string should be made visible in one single cell by putting breaks in the string and also expanding the height of the cell automatically according to number of lines printed.

Following is the relevant part of my PHP code. I am also attaching a screenshot of generated PDF to give better idea.

Issue Column Contents exceeding their designated cell.

    $module = $_POST['module'];
$client = $_POST['client'];
$severity = $_POST['severity'];
$issue = $_POST['issue'];
$date = date('Y-m-d',strtotime($_POST['date']));
mysql_select_db($database);
$res = mysql_query("select * from parent");

require("fpdf/fpdf.php");
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFillColor(224,235,255);
$pdf->SetFont("Arial", "B", "20");
$pdf->Cell(187,15, "Detailed Bugs Report",1,0,C);
$pdf->Ln();
$pdf->Ln();
$pdf->SetFont("Times","B","15");

        $pdf->Cell(48,10, "MODULE",1,0,C); 
    $pdf->Cell(22,10, "CLIENT",1,0,C); 
    $pdf->Cell(30,10, "SEVERITY",1,0,C); 
    $pdf->Cell(27,10, "DATE",1,0,C); 
    $pdf->Cell(60,10, "ISSUE",1,0,C);
        $pdf->Ln();
$pdf->SetFont("Arial","","12");
while($row=mysql_fetch_row($res))
{
    $pdf->Cell(48,10, "$row[0]",1,0); 
    $pdf->Cell(22,10, "$row[1]",1,0,C); 
    $pdf->Cell(30,10, "$row[2]",1,0,C); 
    $pdf->Cell(27,10, "$row[3]",1,0,C);
        //$cellWidth = $pdf->GetStringWidth($row[4]);
        $pdf->Cell(60, 10, $row[4], 1, 0); 
        $pdf->Ln();
}
$pdf->Output();
2

There are 2 answers

2
Olli On

You can use MultiCell instead of Cell to put your text in. It should add a line break at the end of the cell and wrap the text automatically or you can use \n as a line break to explicitly wrap the text.

0
Vishal Khare On

Why is there spaces before initial words and more than usual spaces. it is still somewhat exceeding the boundary of cell. Still exceeding the cell boundary