【发布时间】:2021-06-19 16:37:47
【问题描述】:
我正在尝试学习如何使用 PHP 语言使用 FPDF 和 FPDI 编辑 PDF 文件。我有这个sample PDF file,我想在其中插入一些值。如果我要使用 FPDF 创建 PDF 文件,一切正常。但是如果我尝试使用 FPDI 编辑现有的 PDF 文件,它会给我以下错误消息:This page isn’t working. Failed to load resource: the server responded with a status of 500 ( ). crbug/1173575, non-JS module files deprecated. 下面显示了两个用于编辑 PDF 文件的程序。第一个有效,第二个失败。
<?PHP
//This program works and create a new PDF file.
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>
以下用于编辑现有 PDF 文件的程序失败:
<?PHP
require_once('fpdf.php');
//This second line of code fails.
require_once('../fpdf183/FPDI/FPDI-2.3.6/src/Fpdi.php'); //This path is correct. I have tested it out with a simple echo “hello world”; file.
//I assume I didn’t install fpdi correctly. I have tried replacing /Fpdi.php part with autoload.php as well.
$pdf = new FPDI();
$pdf->AddPage();
$pdf->setSourceFile('testfile.pdf');
$tplIdx = $this->pdf->importPage(1); // import page 1
$this->pdf->useTemplate($tplIdx, 0, 0, 0, 0, true);
$this->pdf->SetFont('Arial', '', '13');
$this->pdf->SetTextColor(0,0,0);
$this->pdf->SetXY(20, 20); //set position in pdf document
$this->pdf->Write(0, 'Some texts will go in here');
$this->pdf->Output('newfile.pdf', 'D');//force the browser to download the output
?>
首先,我从fpdf.org 下载了 FPDF .zip 文件并将其解压缩到我的托管文件服务器中。然后我从setasign.com 网站下载了 FPDI,并将这个 .zip 文件上传到我的托管商在线网络服务器。然后我将它提取到同一个 fpdf 文件夹中。 (我的服务器中启用了 zlib 扩展)。
【问题讨论】:
-
对于您的第二个 PHP(您说它失败的那个),请替换所有
$this->pdf->by$pdf->并重试 -
@KenLee 谢谢你的回复。更改后,我仍然收到相同的错误。如果我删除所有代码并在我的 PHP 文件中保留以下语句
require_once('../fpdf183/FPDI/FPDI-2.3.6/src/Fpdi.php');,我仍然会收到相同的错误: -
你有其他版本的 fpdi 吗? (像这样的:
require_once('vendor/setasign/fpdi/fpdi.php'); -
@KenLee 不,我没有另一个版本和这个 fpdi。我下载的那个也没有名为
vendor/seatasign/..的文件夹。 setasign.com/products/fpdi/downloads -
请在我的回答中查看建议的方式。谢谢