FPDF 无法创建简单的 pdf

FPDF not working in creating a simple pdf

我正在玩 FPDF 库,我尝试了这个简单的代码:

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();

但它什么也没做。没有弹出任何文档或异常。如果我更正,如果一切正常,应该会出现一份文件。我不知道为什么它不起作用。任何帮助将不胜感激:)

您的问题来自 PDF 的页眉功能 Class。根据文档,至少您必须在 Header 函数

上设置这些变量
    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);
}

这是我的代码,看起来像你的代码,而且它的工作原理

<?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();

结果:

更多细节你可以访问FPDF

的官方文档