Anggiet Bracmatya
Anggiet Bracmatya

Reputation: 19

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

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?

Upvotes: 1

Views: 22654

Answers (4)

Amirmasoud
Amirmasoud

Reputation: 658

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());

Upvotes: 1

AKSHAY JAIN
AKSHAY JAIN

Reputation: 1

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

Upvotes: 0

pankaj
pankaj

Reputation: 1906

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

Upvotes: 5

Lawrence Njenga
Lawrence Njenga

Reputation: 326

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

 ...

Upvotes: 7

Related Questions