Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jonassiewertsen's avatar

Download PDF in Laravel Nova Action

I want do generate a pdf in Laravel Nova and download theme. I can't get it working

The code is in an Laravel Nova Action (php artisan nova:action)

public function handle(ActionFields $fields, Collection $models)
{
   $pdf = PDF::loadView('pdf.test');
   
   // This is saving the pdf. So the $pdf->download does work
   // Storage::put('/pdfs/testsave.pdf', $pdf->download('save.pdf')); 

   // NOT Working in the Nova Action
   // return $pdf->download

   // NOVA says to do something like this. 
   return Action::download('url_here', 'file_name.pdf');

    }

Maybe i could save the pdf on the server, pass the path and delete it again?

Any ideas how to get it to work?

In the best possible, i don not need to save the pdf on my server.

Regards, Jonas

0 likes
7 replies
aurawindsurfing's avatar

Hey @jonassiewertsen

I'm late but here is my action to download generated pdf:

<?php

namespace App\Nova\Actions;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;
use Laravel\Nova\Actions\Action;
use Laravel\Nova\Fields\ActionFields;

class InvoiceDownload extends Action
{
    use InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Perform the action on the given models.
     *
     * @param  \Laravel\Nova\Fields\ActionFields  $fields
     * @param  \Illuminate\Support\Collection  $models
     * @return mixed
     */
    public function handle(ActionFields $fields, Collection $models)
    {

        $invoiceController = new \App\Http\Controllers\InvoiceController();
        $created = $invoiceController->generateInvoices($models, false);
        $path = $invoiceController->makePDF($models);

        return Action::download(url($path), uniqid() . '.pdf');

    }

    /**
     * Get the displayable name of the action.
     *
     * @return string
     */
    public function name()
    {
        return ('Create invoice');
    }

    /**
     * Get the fields available on the action.
     *
     * @return array
     */
    public function fields()
    {
        return [

        ];
    }

}

public function makePDF($invoices)
    {
        $pdf = \App::make('dompdf.wrapper');
        $pdf->loadView('invoices.invoice', compact('invoices'));
        $id = uniqid();
        $path = '/storage/tmp/invoices/'. $id .'.pdf';
        $pdf->save(public_path($path));

        return $path;

    }

I know this is incorrect \App::make('dompdf.wrapper'); but this is how my code works at the moment.

Hope it helps!

2 likes
kidereo's avatar

Hi, @aurawindsurfing, could you please share your code for the InvoiceController to see how you generate your invoices? I am stuck at trying to build pdf files through an event listener, and your makePDF method was very helpful in figuring out how to save the pdf file locally. Unfortunately, I do not seem to be able to download it by calling Laravel's Storage::download() method from within the listener. Perhaps there is a way to do it through a controller...

Hope you can help, many thanks in advance!

aurawindsurfing's avatar

@kidereo to download the file you need to send it to your browser as a response. Similar to what you do to send your browser to a view.

When you click something, just return from your controller Storage::download() that should do the trick.

Remember that you can download only one file that way.

Hope it helps!

kidereo's avatar

@aurawindsurfing Thanks a lot for your explanation! I got the controller involved as well this morning but in the end I too went with a Download document action instead. Seems neater this way. Thanks again!

Mauro19's avatar

@aurawindsurfing I see that you are implementing invoices using laravel nova. How did you manage to solve this problem to create a more friendly create/detail view for invoices within laravel nova? thanks

aurawindsurfing's avatar

@mauro19 sorry for delay, I do not use any special view for creating invoice. Invoice needs:

  1. Company
  2. Client
  3. Date / Number / Place etc
  4. Items

If you look at it that way your realise you do not need a special view that will look like an invoice... it is just a form at the end of the day.

Hope it helps!

Please or to participate in this conversation.