Skip to content
Advertisement

FPDF not working in creating a simple pdf

I’m playing with FPDF library and i try this simple code:

require_once($_SERVER['DOCUMENT_ROOT'].'/fpdi/FPDF/fpdf.php');
class PDF extends FPDF{
    function Header(){
        $this->Write(6,'Dokumen ini adalah sah');
    }
}
$pdf = new PDF();
$pdf->SetFont('Arial');
$pdf->AddPage();
$pdf->SetXY(5, 5);
$pdf->Write(8, 'A complete document imported with FPDI');
// Output the new PDF
$pdf->Output();

But it didn’t do anything. No document or exception is popped out. If i correct, if everything is fine a document should appear. I have no idea why it’s not working. Any help would be very appreciated 🙂

Advertisement

Answer

Your problem comes from Header function on your PDF Class. Based on the documentation at least you have to set these variables on Header function

    function Header()
{
    // Select Arial bold 15
    $this->SetFont('Arial','B',15);
    // Move to the right
    $this->Cell(80);
    // Framed title
    $this->Cell(30,10,'Title',1,0,'C');
    // Line break
    $this->Ln(20);
}

And this is my code that looks like your code, and its works

<?php
require __DIR__ . '/vendor/autoload.php';

class PDF extends FPDF{
    function Header()
{
    // Select Arial bold 15
    $this->SetFont('Arial','B',15);
    // Move to the right
    $this->Cell(80);
    // Framed title
    $this->Cell(50,10,'Dokumen ini sah',1,0,'C');
    // Line break
    $this->Ln(20);
}

}

$pdf = new PDF();

// print_r($pdf);
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();

Result : enter image description here

More detail you can visit official documentation of FPDF

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement