I am generating a table by using FPDF library as a PDF document generator.
The problem is the table that contain the data won't centered although I've tried to write 'C' in the $pdf->Cell() parameter. The output is aligned to the left side as shown below.
And also why the cell is moved into the bottom while there're still spaces on the right when I add a new column(5 column) as shown below.
I've tried to add 'C' parameter inside of all the cell related like this
$pdf->Cell(190,7,'Some text',0,1,'C');
,
And also using $pdf->SetXY(20,20);
a solution from this post
But it still doesn't work.
My current code is:
<?php
require('fpdf.php');
$pdf = new FPDF('P','mm','A4');
$pdf->AddPage();
$pdf->Image('some_image.png',10,10,30,15);
$pdf->SetFont('Arial','B',16);
$pdf->Cell(190,7,'Some title',0,1,'C');
$pdf->SetFont('Arial','B',9);
$pdf->Cell(190,7,'Jl. Some address',0,1,'C');
$pdf->Cell(10,7,'',0,1);
$pdf->SetFont('Arial','B',9);
$pdf->Cell(190,7, $somestring1 ,0,1,'C');
$pdf->SetFont('Arial','',9);
$pdf->Cell(190,7,'to',0,1,'C');
$pdf->SetFont('Arial','B',9);
$pdf->Cell(190,7, $somestring2 ,0,1,'C');
$pdf->SetFont('Arial','B',8);
$pdf->Cell(10,7,'',0,1,'C');
$pdf->Cell(6,6,'NO',1,0,'C');
$pdf->Cell(23,6,'TUJUAN',1,0,'C');
$pdf->Cell(25,6,'PEMINJAM',1,0,'C');
$pdf->Cell(35,6,'KENDARAAN',1,0,'C');
$pdf->Cell(27,6,'JAM BERANGKAT',1,1,'C');
// If I add this cell, then i'll overlap/move to the bottom
$pdf->Cell(27,6,'JAM PULANG',1,1,'C');
$pdf->SetFont('Arial','',8);
$query = mysqli_query($someconn, "SELECT somequery");
$i = 1;
while ($row = mysqli_fetch_array($query)){
$pdf->Cell(6,6,$i++,1,0,'C');
$pdf->Cell(23,6,$row['sometable'],1,0,'C');
$pdf->Cell(25,6,$row['sometable'],1,0);
$pdf->Cell(35,6,$row['sometable']." ".$row['sometable'],1,0,'C');
$pdf->Cell(27,6,$row['sometable'],1,1,'C');
$pdf->Cell(30,6,$row['sometable'],1,1,'C');
}
$pdf->Output();
?>
The flowing to the next page appears to be caused by this line
$pdf->Cell(27,6,'JAM BERANGKAT',1,1,'C');
containing a 1 right before the 'C'
(the one says go to the next line). Change it to
$pdf->Cell(27,6,'JAM BERANGKAT',1,0,'C');
and that additional cell should not start on a new line.
For centering, with no margins set your page width should be 210. The width of all of your cells is 153 which means that should fit just fine.
To center things on the page use SetLeftMargin
to half of the difference between the page width, 210, and the total width of your columns, 153 which comes out to 28. Right after you create the instance of FPDF set the margin before starting a new page.
$pdf = new FPDF('P','mm','A4');
$pdf->SetLeftMargin(28);
$pdf->AddPage();