Search code examples
phppdffpdf

How do I define SetXY and Cell with FPDF PHP Class?


I recently went to this tutorial who explains how to create a PDF file with FPDF PHP Class. Everything works great but I have some questions that this blog does not answer properly.

http://wpeasytuts.com/design-create-pdf-with-php/

On the last part, the author is saying :

    //Add Name for Invoice
$pdf->SetXY(31,37);
$pdf->SetFillColor(255,255,255);
$pdf->SetFont('Arial','',9);
$pdf->Cell(50, 4, "John Doe", 0, 1,'L',1);

How do he knows this paramaters ? How do I know those parameters are correct if I want to create my own pdf file ? How does he knows when the pdf is generated it is (31,37) ? same for the $pdf ->cell (50, 4, "John DOE" ... )

How do we find the right number ? I checked online, on google, everywhere. Is there is a tool or some website explaining how to find those right number ?

$pdf->SetXY(31,37);

How does it know it is 31 How does it know it is 37 ?

Where can I find this trick to place it correctly ?

Someone knows ?


Solution

  • You are right to query that minimal code example as we will see there is a misleading error in my measuring that perplexed me when I tried to emulate what you were given.

    You want to know where the location numbers come from, and in a way that's eventually decided by yourself when you build your own "Template".

    Here I am using the example given and we can see from the values at lower border (sorry they are cm so multiply by 10 for mm) that the 9 point high text would need to be positioned to the right of Name: at an arbitrary 31 mm from left edge and also 37 from top edge.

    As to why 50,4 well there is a mistake on my part as my emulation says it should be minimal 50,14 and for "Quasimo Doe" it would need to be much longer than 50, so suggested it could be much better if the last line was

    $pdf->Cell(150, 14, "John Doe", 0, 1,'L',1);
    

    That assumption turned out to be wrong. The problem is relative units and if we use the border values 50 ticks by 4 ticks in page units would be adequate.

    enter image description here

    enter image description here For more info on the syntax of that command (and others) see http://fpdf.org/en/doc/cell.htm