FPDF 获取“输出目标不正确”,但错误代码显示正确的目标

Mar*_*bov 7 php fpdf fpdi laravel vue.js

我正在尝试使用 FPDF 和 FPDI 编辑 PDF 并向其添加文本。我不断收到“输出目标不正确”错误,但目标是我希望它在其中创建文件的正确位置,为什么 FPDF 不喜欢我的输出目标?

这是在 laravel 项目中

    $pdf = new \setasign\Fpdi\Fpdi();
    $pdf->AddPage();
    $pdf->setSourceFile(public_path('/pdf/higher.pdf'));
    $tplIdx = $pdf->importPage(1);
    $pdf->useTemplate($tplIdx, 10, 10, 100);
    $pdf->SetFont('Helvetica');
    $pdf->SetTextColor(255, 0, 0);
    $pdf->SetXY(30, 30);
    $pdf->Write(0, 'This is just a simple text');
    $pdf->Output(public_path('/pdf/'),'higher2');
    return $pdf;
Run Code Online (Sandbox Code Playgroud)

错误是:

 message: "FPDF error: Incorrect output destination: /home/vagrant/code/project-name/public/pdf/"
Run Code Online (Sandbox Code Playgroud)

我还尝试删除“public_path()”并将其设置为,Output('pdf', 'higher2')但那里也没有好处。

此外,我还尝试更改输出 pdf 的名称,higher2.pdf以防万一它想查看扩展名(但显然它更多的是目的地问题,而不是名称问题)

我什至尝试更改此文件夹的权限,使其可供任何人写入:

drwxrwxrwx  5 ion  staff    160 May 21 05:44 pdf
Run Code Online (Sandbox Code Playgroud)

编辑:请注意,我看到 public_path() 的方法由于某种原因试图保存到我的 vagrant 文件夹中,这是我感到困惑的部分原因。当我尝试在没有 public_path() 的情况下保存到“/pdf”时,出现此错误:

 message: "FPDF error: Incorrect output destination: /pdf/"
Run Code Online (Sandbox Code Playgroud)

编辑2:

我也尝试过这个:

$pdf->Output('F','/pdf/higher2.pdf');
Run Code Online (Sandbox Code Playgroud)

并得到错误:

message: "file_put_contents(/pdf/higher2.pdf): failed to open stream: No such file or directory"
Run Code Online (Sandbox Code Playgroud)

并尝试了确实存在的pdf的原始名称并得到了相同的错误:

$pdf->Output('F','/pdf/higher.pdf');
Run Code Online (Sandbox Code Playgroud)

Jan*_*bon 10

你不应该覆盖你正在读取的文件!

该方法的签名Output()是:

string Output([string dest [, string name [, boolean isUTF8]]])
Run Code Online (Sandbox Code Playgroud)

参数$dest定义为:

发送文档的目的地。它可以是以下之一:

I: send the file inline to the browser. The PDF viewer is used if available.
D: send to the browser and force a file download with the name given by name.
F: save to a local file with the name given by name (may include a path).
S: return the document as a string.
Run Code Online (Sandbox Code Playgroud)

默认值为 I。

所以你的代码:

$pdf->Output(public_path('/pdf/'),'higher2');
Run Code Online (Sandbox Code Playgroud)

完全没有意义。我猜你想将生成的 PDF 保存到公共区域中名称为 的路径中higher2.pdf。所以你的代码应该是这样的:

$pdf->Output('F', public_path('/pdf/higher2.pdf'));
Run Code Online (Sandbox Code Playgroud)

PS:您无法使用 FPDI 编辑 PDF