7

It seems like the order of SetX() and SetY() does matter. As you can see, the second cell-box in the example is located at following coordinates: X:10.00125/Y:80. Actually it should be at x=80. Setting Y-coordinate first fixes the problem. Is it a bug? PHP version used is 5.3.28.

<?php
require('./fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);

$pdf->SetY(50);
$pdf->SetX(80);
$pdf->Cell(0,5,'Coordinates: X:'.$pdf->GetX().'/Y:'.$pdf->GetY(), 1); 

$pdf->SetX(80);
$pdf->SetY(80);
$pdf->Cell(0,5,'Coordinates: X:'.$pdf->GetX().'/Y:'.$pdf->GetY(), 1); 

$pdf->Output();
?>
Thorsten
  • 71
  • 1
  • 4

1 Answers1

12

This is obvious. Look at the source or the manual:

function SetY($y)
{
    // Set y position and reset x
    $this->x = $this->lMargin;
    if($y>=0)
        $this->y = $y;
    else
        $this->y = $this->h+$y;
}

So this seems to be no bug. x is reset to the left-margin, what you already noticed. You could use SetXY($x, $y) instead.

I think they wanted to have SetY to be used for placing the next paragraph, so its always aligned to the left side.

Lars M.
  • 179
  • 10
  • Thanks! - I should have looked into the code before asking. – Thorsten Apr 16 '14 at 15:05
  • 6
    Wow, that's... really really shitty. A method called SetY should have no effect on X. Especially not if there's not a doc block that tells you that. – GordonM Sep 14 '16 at 09:34
  • 1
    They added a boolean so that you can disable this,so you can call $pdf->setY($y,false) and disabe resetting the x parameter – Iruku Kagika Jan 26 '19 at 09:44