【问题标题】:Getting 'Cannot open pdf' error with a correct source url when using FPDI使用 FPDI 时使用正确的源 URL 获取“无法打开 pdf”错误
【发布时间】:2019-10-09 02:23:22
【问题描述】:

我正在尝试在 laravel 中编辑 pdf。我在控制器中创建了以下函数,其中有 use FPDIuse FPDF

static function getHigherPDF() {

    $pdf = new FPDI();
    $pdf->AddPage();
    $pdf->setSourceFile('/pdf/higher.pdf');
    $tplIdx = $pdf->importPage(1);
    $pdf->useTemplate($tplIdx, 10, 10, 100);
    $pdf->Output();
    $pdf->SetFont('Helvetica');
    $pdf->SetTextColor(255, 0, 0);
    $pdf->SetXY(30, 30);
    $pdf->Write(0, 'This is just a simple text');

    return $pdf;

}

我不断收到以下错误,我不知道为什么,如果我将带有 http:// 等的完整路径放入 pdf,我什至会收到错误。当我转到该文件时,pdf 在我的浏览器中打开。如果文件 url 是有效的 url,我找不到任何关于为什么会发生这种情况的信息

有什么想法吗?

{message: "Cannot open /pdf/higher.pdf !", exception: "InvalidArgumentException",...}
exception: "InvalidArgumentException"
file: "/home/vagrant/code/brainskills-at-work/vendor/setasign/fpdi/pdf_parser.php"
message: "Cannot open /pdf/higher.pdf !"

【问题讨论】:

  • 通过表明$pdf->setSourceFile('/pdf/higher.pdf'); 您引用的是绝对路由,您必须使用相对路由。你的 pdf 在哪里?
  • 它位于http://www.sample.com/pdf/higher.pdf
  • 我的意思是,在你的文件系统中,你的 laravel 项目中有什么文件夹? storage/app/public?
  • 嗯,文件夹结构中的路径是root/public/pdf/higher(不在存储中)

标签: php laravel pdf-generation fpdf fpdi


【解决方案1】:

您应该使用 laravel 生成器为公共路径引用您的文件:

$pdf->setSourceFile(public_path('/pdf/higher.pdf'));

这将在您的文件系统上生成文件的绝对路径。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-22
    • 2020-11-15
    • 1970-01-01
    • 2016-07-15
    • 1970-01-01
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多