【问题标题】:how to save dompdf file to storage and name the file dynamicly in laravel如何将dompdf文件保存到存储并在laravel中动态命名文件
【发布时间】:2020-07-02 20:11:12
【问题描述】:

有谁知道如何保存生成的 pdf 文件并在 laravel 中使用 dompdf 动态命名它? 我总是遇到这个错误

failed to open stream: No such file or directory

在尝试生成 pdf 文件并将其保存到项目目录时。 这是我的控制器代码

$bills = Tagihan::join('pesanan', 'tagihan.id_pesanan', 'pesanan.id_pesanan')
                    ->join('kendaraan', 'pesanan.id_kendaraan', 'kendaraan.id_kendaraan')
                    ->join('kategori_mobil', 'kendaraan.id_kategori_kendaraan', 'kategori_mobil.id_kategori')
                    ->join('users', 'pesanan.id_pengguna', 'users.id')
                    ->where('pesanan.id_pesanan', $save->id_pesanan)
                    ->select('pesanan.*', 'users.*', 'tagihan.*', 'kendaraan.*', 'kategori_mobil.nama_kategori')
                    ->first();
    $today = Carbon::now('GMT+7')->toDateString();
      $pdf = new PDF();
      $pdf = PDF::loadView('admin.tagihan.pdf', compact('bills','today'));
      file_put_contents('public/bills/'.$noOrder.'.pdf', $pdf->output() );
    return redirect()->route('pesananIndex');

我想用 $noOrder.'pdf' 的自定义字符串保存我的 pdf 文件,但是当我使用这样的静态名称时

file_put_contents('public/bills/bubla.pdf', $pdf->output());

我没有错误,有什么解决办法吗?

【问题讨论】:

  • 为什么是 file_put_contents?

标签: laravel laravel-5 save dompdf


【解决方案1】:

您可以从 http 响应中获取原始内容到变量中,然后将其推送到您的文件中。

 $pdf = PDF::loadView('admin.tagihan.pdf', compact('bills','today'));

 $content = $pdf->download()->getOriginalContent();
 Storage::put('public/bills/bubla.pdf',$content);

 ...

【讨论】:

  • 在上面的例子中是admin.tagihan.pdf文件已经被domPdf渲染了吗?
猜你喜欢
  • 1970-01-01
  • 2020-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
相关资源
最近更新 更多