首页 > 解决方案 > FPDF无法创建简单的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();

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

标签: phppdffpdf

解决方案


您的问题来自 PDF 类上的 Header 功能。根据文档,至少您必须在 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的官方文档


推荐阅读