Search code examples
phpcellfpdf

How to format a single cell in a multicell fpdf table


I'm using a multicell table with fpdf and I want to format the value of a cell with bold letters depending on a condition

Example:

$pdf->SetFont('Arial','B',10);
$pdf->Row(array("","IDUSER","NAME","AGE"); //header of the table<BR>
do {
    if($myrow["age"] >30)
         $age=<b>$myrow["age"]; //value in bold letter<br>
    else
        $age=$myrow["age"];

    $pdf->Row(array($n,$myrow["id_user"],$myrow["name"],$age));

} while ($myrow = mysql_fetch_array($result));

Solution

  • Just call SetFont with 'B' to get bold and without 'B' for regular before displaying text :

    $pdf->SetFont('Arial','B',10);
    $pdf->Row(array("","IDUSER","NAME","AGE"); //header of the table<BR>
    do<br>
    {<BR>
        if($myrow["age"] >30)<br>
          { $pdf->SetFont('Arial','B',10);  // ◄■■■■■■■■■■■■■ BOLD
            $age=$myrow["age"]; //value in bold letter<br>
          }
        else{ <br>
              $pdf->SetFont('Arial','',10);  // ◄■■■■■■■■■■■■■ REGULAR.
              $age=$myrow["age"];<br>
            } 
        $pdf->Row(array($n,$myrow["id_user"],$myrow["name"],$age));<br>
    
    } while ($myrow = mysql_fetch_array($result);<br>
    

    Another approach :

    $pdf->SetFont('Arial','B',10);
    $pdf->Row(array("","IDUSER","NAME","AGE"); //header of the table<BR>
    do<br>
    {<BR>
        if($myrow["age"] >30)<br>
          { $font = "B"; // ◄■■■ BOLD FONT.
            $age=$myrow["age"]; //value in bold letter<br>
          }
        else{ <br>
              $font = ""; // ◄■■■ REGULAR FONT.
              $age=$myrow["age"];<br>
            } 
        $pdf->SetFont('Arial',$font,10); // ◄■■■ SET FONT.
        $pdf->Row(array($n,$myrow["id_user"],$myrow["name"],$age));<br>
    
    } while ($myrow = mysql_fetch_array($result);<br>