0

I am trying to position a table in the center of my PDF using FPDF, searching the internet I did not find any solution.

The table you make is the one explained in the examples on the official page. (Modified to load data using a database, but the idea is the same)

Do you know any way to position the table in the center?

class PDF extends FPDF {

   function FancyTable($header, $data) {
   $this->SetFillColor(255,0,0);
   $this->SetTextColor(255);
   $this->SetDrawColor(128,0,0);
   $this->SetLineWidth(.3);
   $this->SetFont('','B');

   $w = array(40, 35, 45, 40);
   for($i=0;$i<count($header);$i++)
       $this->Cell($w[$i],7,$header[$i],1,0,'C',true);
   $this->Ln();
   $this->SetFillColor(224,235,255);
   $this->SetTextColor(0);
   $this->SetFont('');

   $fill = false;
   foreach($data as $row) {
       $this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);
       $this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);
       $this->Cell($w[2],6,number_format($row[2]),'LR',0,'R',$fill);
       $this->Cell($w[3],6,number_format($row[3]),'LR',0,'R',$fill);
       $this->Ln();
       $fill = !$fill;
   }

   $this->Cell(array_sum($w),0,'','T');
     }
 }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0

You have the page width via $this->GetPageWidth() and you have the width of the table array_sum($w).

So just set the left margin accordingly before writing the table:

$lMargin = ($this->GetPageWidth() - array_sum($w)) / 2;
$this->SetLeftMargin($lMargin);
Jan Slabon
  • 4,736
  • 2
  • 14
  • 29