【问题标题】:FPDF Getting "Incorrect output destination" but the error code showing the correct destinationFPDF获取“不正确的输出目标”,但错误代码显示正确的目标
【发布时间】:2019-10-10 04:33:54
【问题描述】:

我正在尝试使用 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;

错误是:

 message: "FPDF error: Incorrect output destination: /home/vagrant/code/project-name/public/pdf/"

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

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

我什至尝试将这个文件夹的权限更改为任何人都可以写:

drwxrwxrwx  5 ion  staff    160 May 21 05:44 pdf

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

 message: "FPDF error: Incorrect output destination: /pdf/"

编辑 2:

我也试过这个:

$pdf->Output('F','/pdf/higher2.pdf');

并得到错误:

message: "file_put_contents(/pdf/higher2.pdf): failed to open stream: No such file or directory"

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

$pdf->Output('F','/pdf/higher.pdf');

【问题讨论】:

    标签: php laravel vue.js fpdf fpdi


    【解决方案1】:

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

    Output() 方法的签名是:

    string Output([string dest [, string name [, boolean isUTF8]]])
    

    $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.
    

    默认值为 I。

    所以你的代码:

    $pdf->Output(public_path('/pdf/'),'higher2');
    

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

    $pdf->Output('F', public_path('/pdf/higher2.pdf'));
    

    PS:You cannot edit a PDF with FPDI!

    【讨论】:

    • 啊哈,以这种方式添加公共路径有效!出于某种原因,如果我将其设置为Output('F', '/higher2.pdf'),它将保存到根文件,但如果我将其设置为 ``Output('F', '/pdf/higher2.pdf') 它将无法工作 谢谢!至于编辑,我只是​​说我正在使用 FPDI 在上面添加文本
    • 你应该阅读this关于相对和绝对路径的信息;-)也欢迎你将我的答案标记为已接受。
    • 谢谢!我正在使用 mpdf,而“Destination”是我心中的一个目录。不是输出类型。感谢您的回答,我现在知道“目的地”和输出类型不明确。
    • 我认为这在较新版本的 tFPDF 中有所改变。在旧版本中,路径是第一个参数。
    【解决方案2】:

    Output() 方法要求第一个参数是目标,第二个参数是文件名。

    来自文档:

    F: 保存到本地文件,使用 name 给出的名称(可能包含路径)。

    试试这个:

    $filename="/pdf/higher2.pdf";
    $pdf->Output($filename,'F');
    

    【讨论】:

    • 所以我也试过了,出于某种原因,如果我将文件名设为"/higher2.pdf",但如果我尝试将其放入我的/pdf 文件夹中,它就不行,知道为什么吗?
    • @Maribov 文件夹pdf 存在吗?
    • "[...]第一个参数是目标,第二个参数是文件名。"但是为什么代码示例中的参数是互换的呢?
    • 文件夹 pdf 确实存在于如果我将其保留为 "/filename.pdf" 则保存到的同一文件夹中
    【解决方案3】:

    对于FPDF 包,$pdf->Output('F','/pdf/higher2.pdf'); 的语法是错误的,您需要按照 Jan Slabon 的说明调整您的调用。

    但是,如果你想支持 UTF-8 字符,那么你需要 tFPDF 包,这也是 setasign 供应商支持的:

    $pdf = new \setasign\Fpdi\Tfpdf\Fpdi();
    

    对于这个包,你可以像这样存储输出:

    $pdf->Output('/pdf/higher2.pdf');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-15
      • 2015-05-11
      • 1970-01-01
      • 2011-12-21
      • 2017-09-17
      • 1970-01-01
      • 2014-09-07
      • 1970-01-01
      相关资源
      最近更新 更多