php - 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();
但它什么也没做。没有弹出文档或异常。如果我正确,如果一切正常,应该会出现一个文件。我不知道为什么它不起作用。任何帮助将不胜感激:)
解决方案
您的问题来自 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的官方文档
推荐阅读
- asp.net - ASP.NET Core 2.2 Razor 页面中的自定义路由
- c# - C#中Random类的统计定义
- c# - EPPlus C# x 轴类型“文本轴”
- home-assistant - 无法使用 strptime 函数在家庭助理/hass.io 中格式化时间戳传感器
- couchbase - java.lang.IllegalStateException: 这个 Observable 的内容已经发布
- linux - 在 yocto 中使用 llvmpipe 而不是 softpipe
- python - 如何使用 while 循环从用户输入中附加任意数量的键和多个值的字典
- oracle-apex - 为什么 APEX 的页面加载需要很长时间:“...不保存:相同的值/密码/无会话”
- python - 是否有任何 tensorflow 函数可以返回类似于 scikit-learn .describe() 或 .info() 方法的数据统计信息?
- javascript - vuejs登录后无法重定向