Danny Tonks
Danny Tonks

Reputation: 19

FPDF Cell Not Centering On Page PHP

i'm using guys i'm using FPDF to create a page and have it set up where it creates two lines both centered one with the text "line1" and the other "line2". The issue i'm having is that the first one works as expected but the second is offset to the right of the page, does anyone know where I am going wrong here?

Example:

 _______________
|     Line1     |
|          Line1|
|               |
|               |
|               |
|_______________|

PHP:

require('fpdf.php');

$pdf = new FPDF();
$pdf->AddPage();

$pdf->SetFont('Arial','UB',32);
$pdf->Cell(0,10,'Line1',0,0,'C');

$pdf->SetFont('Arial','',22);
$pdf->Cell(0,40,'Line2',0,0,'C');

$pdf->Output();

Upvotes: 1

Views: 1788

Answers (1)

KIKO Software
KIKO Software

Reputation: 16688

FPDF works with an current position. In the manual for Cell() it says:

The upper-left corner of the cell corresponds to the current position. The text can be aligned or centered. After the call, the current position moves to the right or to the next line.

See: http://www.fpdf.org/en/doc/cell.htm

In your case the position has moved to the right. You can set the current position with SetXY(<x>,<y>);. So you can change your code to:

require('fpdf.php');

$pdf = new FPDF();
$pdf->AddPage();

$pdf->SetXY(20,20); 

$pdf->SetFont('Arial','UB',32);
$pdf->Cell(0,10,'Line1',0,0,'C');

$pdf->SetXY(20,40); 

$pdf->SetFont('Arial','',22);
$pdf->Cell(0,10,'Line2',0,0,'C');

$pdf->Output();

You can, of course, adapt it to your own needs.

Upvotes: 1

Related Questions