PHP 7 FPDF Example to Insert HTML5 Table With Custom Column Width Inside PDF Document Using Javascript

You are currently viewing PHP 7 FPDF Example to Insert HTML5 Table With Custom Column Width Inside PDF Document Using Javascript

PHP 7 FPDF Example to Insert HTML5 Table With Custom Column Width inside PDF Document Using Javascript

<?php
  require('fpdf/fpdf.php');
 
  class PDF extends FPDF
  {
    function createTable($header,$data){
      $width=[40,60,30];
      $this->SetFont('Arial','B',12);
      for($i=0;$i<count($header);$i++){
        $this->Cell($width[$i],7,$header[$i],1);
      }
      
      $this->Ln();
      $this->SetFont('Arial','',12);
      foreach($data as $row){
        $this->Cell($width[0],7,$row[0],1,0);
        $this->Cell($width[1],7,$row[1],1,0);
        $this->Cell($width[2],7,$row[2],1,0);
        $this->Ln();
      }
    }
  }
 
  $pdf=new PDF();
  $pdf->AddPage();
  //Table Header
  $header=["Name","Email","Age"];
  //Table Rows
  $data=[
    ["Ram","ram@gmail.com",25],
    ["Sam","sam@gmail.com",21],
    ["Kumar","kumar@gmail.com",33],
    ["Bala","bala@gmail.com",35],
    ["Raj","raj@gmail.com",28],
    ["Tom","tom@gmail.com",30],
  ];
  $pdf->createTable($header,$data);
  $pdf->Output();
?>

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply