I’m playing with FPDF library and i try this simple code:
JavaScript
x
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
JavaScript
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
JavaScript
<?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();
More detail you can visit official documentation of FPDF