line height auto adjust in fpdf multicell ()

15,933

Solution 1

Auto line height is not possible in FPDF MultiCell. As This page explains, Multicell creates a cell for each line of text you print. The 2e argument (h) is the height of each cell. (the line height).

Is there a better solution?

Yes there is. The people from TCPDF has taken the FPDF class and extended/improved it. They support HTML output. There you can use html for your outputs. Maybe you can use that.

Solution 2

$line_height = 5;
$width = 189;
$text = ("Your text goes here");    
$height = (ceil(($pdf->GetStringWidth($text) / $width)) * $line_height);

$pdf->Multicell($width,$height,$text,1,1);

You can play with the values of $line_height and $width to fit with your font/margins/whatever you need to do, but the formula for height will calculate the number of lines needed and multiply that number by the height of each line.

Share:
15,933
Aditya Kumar Singh Tomar
Author by

Aditya Kumar Singh Tomar

Updated on June 18, 2022

Comments

  • Aditya Kumar Singh Tomar
    Aditya Kumar Singh Tomar about 2 years

    I am using fpdf to create pdf from data in MySql table dynamically. And I am printing it using MultiCell() function. My Problem is I want it to auto adjust the line height of the text. I have read the documentation at the site but it only tells about auto adjust width. But I could not find any solution for line height auto adjust. Is there any way to do so. What may be the alternate ways? Please help. Here is the piece of the code. :-

    <?php session_start();
    
    include 'conn.php';
    $table=$_SESSION["name"];
    $testid=$_SESSION["id"];
    $stuid=$_SESSION["stuid"];
    $ans=$_SESSION["score1"];
    
    //pdf creation
    require('../fpdf/fpdf.php');
    
    class PDF extends FPDF
    {
    function Header()
    {
    global $title;
    $this->Image('../fpdf/logo.JPG',10,10,200);
    // Arial bold 15
    $this->SetFont('Arial','B',15);
    // Calculate width of title and position
    $w = $this->GetStringWidth($title)+6;
    $this->SetX((210-$w)/2);
    // Colors of frame, background and text
    
    // Thickness of frame (1 mm)
    $this->SetLineWidth(1);
    // Title
    // Line break
    $this->Ln(20);
    }
    
    function Footer()
    {
    // Position at 1.5 cm from bottom
    $this->SetY(-15);
    // Arial italic 8
    $this->SetFont('Arial','I',8);
    // Text color in gray
    $this->SetTextColor(128);
    // Page number
    $this->Cell(0,10,'Page '.$this->PageNo(),0,0,'C');
    }
    
    function ChapterTitle($num, $label)
    {
    // Arial 12
    $this->SetFont('Arial','',12);
    // Background color
    $this->SetFillColor(200,220,255);
    // Title
    $this->Cell(0,6,"Chapter $num : $label",0,1,'L',true);
    // Line break
    $this->Ln(4);
    }
    
    function ChapterBody($file)
    {
    // Read text file
    $txt = file_get_contents($file);
    // Times 12
    $this->SetFont('Times','',12);
    // Output justified text
    $this->MultiCell(0,5,$txt);
    // Line break
    $this->Ln();
    // Mention in italics
    $this->SetFont('','I');
    $this->Cell(0,5,'(end of excerpt)');
    }
    
    function PrintChapter($num, $title, $file)
    {
    $this->AddPage();
    $this->ChapterTitle($num,$title);
    $this->ChapterBody($file);
    }
    }
    
    $pdf = new PDF();
    $pdf->SetAuthor('OSP Classes');
    $pdf->AddPage();
    $x=1;
    $i=0;
    $sql1=mysql_query("select * from $table") or die (mysql_error());
    while($result1=mysql_fetch_row($sql1))
    {
    $pdf->SetFont('Arial','B',14);
    $pdf->MultiCell(0,10,'Ques No.'.$x .'.'. $result1[1]);
    $pdf->Ln(10);
    $pdf->SetFont('Arial','',14);
    $pdf ->MultiCell(0,0,'Ans 1.'. $result1[2]);
    $pdf->Ln(10);
    $pdf->SetFont('Arial','',14);
    $pdf ->MultiCell(0,0,'Ans 2.'. $result1[3]);
    $pdf->Ln(10);
    $pdf->SetFont('Arial','',14);
    $pdf ->MultiCell(0,0,'Ans 3.'. $result1[4]);
    $pdf->Ln(10);
    $pdf->SetFont('Arial','',14);
    $pdf ->MultiCell(0,0,'Ans 4.'. $result1[5]);
    $pdf->Ln(10);
    $pdf->MultiCell(0,0,'Right Ans . '. $result1[6].'           '.'Your Ans . '. $ans[$i]);
    $pdf->Ln(20);
    $x++;
    $i++;
    }
    $pdf->Output();
    ?>