Search code examples
phpfpdf

Align the text in pdf correctly using fpdf


I have to create a pdf file. I am using fpdf. My pdf file should look like this:

This is a sample pdf.
Hello world.
sample text sample text.

But these lines are displayed in undesired manner.

My code is

require('fpdf.php');

$pdf = new FPDF('P','mm','A4');
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(0,10,'This is a sample pdf.');
$pdf->Cell(0,30,'Hello world.');
$pdf->Cell(0,50,'sample text sample text.');
$pdf->Output(); 

the resulted pdf is looked like this:

enter image description here

How can I align the text in pdf correctly?


Solution

  • From FPDF documentation about Cell() function:

    Cell(float w [, float h [, string txt [, mixed border [, int ln [, string align [, boolean fill [, mixed link]]]]]]])
    

    After the call, the current position moves to the right or to the next line.

    ln Indicates where the current position should go after the call. Possible values are:

        0: to the right
        1: to the beginning of the next line
        2: below
    

    Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.

    Try this:

    $pdf->Cell(0,10,'This is a sample pdf.', 0, 1);
                                             ^ no border
                                                ^ after the call move to beginning of next line