Search code examples
phpfpdf

FPDF A4 size is not printed


I use FPDF for PDF generation in my project. I want to generate A4 size page and I use following line.

$fpdf = new PDF('P','mm',array(595.28,841.89));
$fpdf->AddPage('P', 'A4'); 

I generate the PDF using this code and I got print out. Then I realize that the page size is not fit for the A4.
Can somebody help me on this regard?


Solution

  • The problem is that you pass the wrong default page size (595.28mm by 841.89mm) to the FPDF constructor, which by the way should be FDPF. A4 is 210mm by 297mm if in portrait mode.

    So you should call the constructor this way:

    $pdf = new FPDF('P','mm','A4');
    

    or

    $pdf = new FPDF('P','mm',array(210,297));
    

    After that you can omit the page size parameter when calling AddPage().

    Here is a complete Hello World example:

    <?php
    require('fpdf.php');
    
    $pdf = new FPDF('P','mm','A4');
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    $pdf->Cell(40,10,'Hello World!');
    $pdf->Output();
    ?>