Cell/Multicell fpdf wrap in codeigniter 3

Please help, I am creating an invoice with FPDF in CodeIgniter, I have a problem when I use CELL, when the text exceeds the cell the text will cross the line.

and when I use MultiCell, the appearance will change.

how to solve it so that when ‘FULL NAME’ and ‘COURSE NAME’ exceed the length of the column it will automatically wrap and the other columns follow the column height of ‘full name’ and ‘course name’

error_reporting(0); // AGAR ERROR MASALAH VERSI PHP TIDAK MUNCUL
        $pdf = new FPDF('p','mm', 'A4');
        $pdf->AddPage();
        // $pdf->Image($background, 0, 0, 210);
        // setting jenis font dan posisi
        
        $pdf->SetFont('Arial','B',15).$pdf->SetTextColor(8, 182, 201);
        $pdf->Cell(0,60,'',0,1);
        $pdf->Cell(0,5,'Invoice',0,1,'C');
        // ATTENTION
        $pdf->SetFont('Arial','',8);
        $pdf->Cell(150,5,'Attention To:',0,0,'');
        $pdf->Cell(50,5,'Invoice No: '.$invoice,0,1,'');        
        $pdf->Cell(150,5,$uni['pic_uni'],0,0,'');
        $pdf->Cell(50,5,'Date: '.date('d F Y', strtotime($data['date_created'])),0,1,'');
        $pdf->Cell(80,5,$uni['email_uni'],0,1,'');
        $pdf->Cell(80,5,$uni['universitas'],0,1,'');
        $pdf->MultiCell(60,5,$uni['alamat_uni'],0,1,'');
        
        // TABLE HEADER
        $pdf->Cell(0,8,'',0,1);
        $pdf->SetFont('Arial','',8).$pdf->SetTextColor(0, 0, 0).$pdf->SetDrawColor(181, 181, 181);
        $pdf->Cell(23,8,'Passport No',1,0,'C');
        $pdf->Cell(40,8,'Full Name',1,0,'C');
        $pdf->Cell(45,8,'Course Name',1,0,'C');
        $pdf->Cell(22,8,'Intake',1,0,'C');
        $pdf->Cell(33,8,'Tuition Fee / Commission',1,0,'C');
        $pdf->Cell(8,8,'%',1,0,'C');
        $pdf->Cell(24,8,'Total Commission',1,0,'C');

        // TABLE BODY
        $pdf->Cell(10,8,'',0,1);
        $pdf->SetFont('Arial','',8).$pdf->SetTextColor(0, 0, 0).$pdf->SetDrawColor(181, 181, 181);
        foreach ($detail as $row) {
            $id_nomor_student = $row['id_nomor_student'];
            $course = $this->db->query("SELECT * FROM tb_student_universitas JOIN tb_universitas ON tb_student_universitas.universitas_tujuan = tb_universitas.code_uni WHERE id_nomor_student='$id_nomor_student' ")->result_array();

            $pdf->Cell(23,8,$row['nomor_passpor_student'],1,0,'C');
            $pdf->Cell(40,8,$row['nama_student'],1,0,'C');
            $pdf->Cell(45,8,$course[0]['universitas'],1,0,'C');
            $pdf->Cell(22,8,date('F Y', strtotime($row['intake'])),1,0,'C');
            $pdf->Cell(33,8,$row['tuition_fee_commission'],1,0,'C');
            $pdf->Cell(8,8,$row['persen'],1,0,'C');
            $pdf->Cell(24,8,$row['total_commission'],1,1,'L');
        }

The issue arises because Cell does not support automatic line wrapping when the text exceeds the cell’s width, and MultiCell does not align other cells to follow its height. To solve this problem, you can calculate the height of the MultiCell dynamically and ensure all other cells in the row adjust accordingly. Here’s a step-by-step solution:

Solution: Adjusting Row Height Dynamically

  1. Calculate Row Height: Determine the maximum height of the row based on the content of the MultiCell fields.
  2. Align Other Cells: Use the same calculated height for all columns in the row.

Implementation

Replace the foreach loop where you generate table rows with this adjusted logic:

foreach ($detail as $row) {
    $id_nomor_student = $row['id_nomor_student'];
    $course = $this->db->query("SELECT * FROM tb_student_universitas JOIN tb_universitas ON tb_student_universitas.universitas_tujuan = tb_universitas.code_uni WHERE id_nomor_student='$id_nomor_student' ")->result_array();

    // Define column content
    $passport_no = $row['nomor_passpor_student'];
    $full_name = $row['nama_student'];
    $course_name = $course[0]['universitas'];
    $intake = date('F Y', strtotime($row['intake']));
    $tuition_fee = $row['tuition_fee_commission'];
    $percentage = $row['persen'];
    $total_commission = $row['total_commission'];

    // Determine the height needed for MultiCell columns
    $cell_widths = [23, 40, 45]; // Widths of 'Passport No', 'Full Name', and 'Course Name'
    $line_heights = [8, 8, 8];   // Default line height for each column
    $texts = [$passport_no, $full_name, $course_name];
    $max_lines = 1; // Start with the minimum number of lines

    for ($i = 0; $i < count($texts); $i++) {
        $lines = $pdf->GetStringWidth($texts[$i]) / $cell_widths[$i];
        $max_lines = max($max_lines, ceil($lines));
    }
    $row_height = $max_lines * 8; // Total row height

    // Draw cells
    $pdf->Cell(23, $row_height, $passport_no, 1, 0, 'C');
    $x = $pdf->GetX();
    $y = $pdf->GetY();
    $pdf->MultiCell(40, 8, $full_name, 1, 'C'); // MultiCell for 'Full Name'
    $pdf->SetXY($x + 40, $y); // Reset X to align next column
    $pdf->MultiCell(45, 8, $course_name, 1, 'C'); // MultiCell for 'Course Name'
    $pdf->SetXY($x + 85, $y); // Reset X to align next column
    $pdf->Cell(22, $row_height, $intake, 1, 0, 'C');
    $pdf->Cell(33, $row_height, $tuition_fee, 1, 0, 'C');
    $pdf->Cell(8, $row_height, $percentage, 1, 0, 'C');
    $pdf->Cell(24, $row_height, $total_commission, 1, 1, 'L');
}

Explanation

  1. Calculate the Maximum Row Height:
  • Use GetStringWidth to calculate the number of lines each MultiCell will require.
  • The height of the row is set as max_lines * line_height.
  1. Use MultiCell for Wrapping:
  • Use MultiCell for columns that require wrapping, such as Full Name and Course Name.
  • Ensure other cells align correctly by resetting the cursor position with SetXY.
  1. Maintain Alignment:
  • After every MultiCell, reset the cursor (SetXY) to align the remaining columns.

Expected Result

This will ensure that:

  • Text in Full Name and Course Name wraps within their respective cells.
  • Other columns in the same row match the height dynamically and align properly.

Let me know if you encounter any issues!