how to save dompdf file to storage and name the file dynamicly in laravel
Asked Answered
B

4

1

does anyone know how to save generated pdf file and name it dynamicly using dompdf in laravel? i always encounter this error

failed to open stream: No such file or directory

while trying to generated pdf file and save it to project directory. here its my controller code

$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');

i want to save my pdf file with custom string with $noOrder.'pdf', but when i use static name like this

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

i had no error, any solution?

Birdlime answered 22/3, 2020 at 2:24 Comment(1)
why file_put_contents?Polygamous
J
7

You can getOriginalContent from http response into a variable then push it to your file.

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

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

 ...
Jackhammer answered 22/3, 2020 at 8:5 Comment(2)
In the above example is admin.tagihan.pdf the file already rendered by domPdf?Numerable
@Numerable No, no, he must have spelled it wrong or just picked a weird name. There you must put the path of a Blade view (the html code), so much so that the method is called 'loadView'.Pinball
S
5
 Storage::disk('local')->makeDirectory('/pdf/order');
      $pdf = PDF::loadView('PDF.Order.OrderPDF-dev', ['invoiceDetail'=>$invoiceDetail]);


      $path = 'storage/pdf/order/';
      $pdf->save($path  . $fileName);
      return url($path.$fileName);

In my example first i created folder for PDF. makeDirectory(...) will create folder if exist or not.

$path is location where i want to put my file in Dir. do not use absolute path or public path function here. only define location where you want to create file.

I am creating file in Storage folder. so i added storage/.....

Shackleton answered 28/12, 2022 at 16:46 Comment(0)
D
0

You can use isset function in your blade.php file like for example in top the page u can set Id){ $data=$bills} ?>

Now you can open file as a stream

Diastase answered 15/3, 2024 at 16:8 Comment(0)
T
0

Not sure if anyone still looking for a shorter answer, but this is how I solved it:

Storage::put('pdf-exports/' . Str::random(40) . '.pdf', $pdf->output());
Tindall answered 25/4, 2024 at 21:2 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.