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

Laracast13's avatar

Laravel DOMPDF

Hi, Using DOMPDF

Route

Route::get('/pdf/{invoice}', [App\Http\Controllers\InvoiceController::class, 'getPDF'])->name('invoice.pdf');

Function

    public function getPDF(Invoice $invoice)
    {     
          
        $pdf = PDF::loadView('invoices.print', $invoice );
    
        return $pdf->download('file.pdf');
    }

Blade

            <table class="table"> 
              <tr>
                <td><strong>Payment Day: {{$invoice->date}}</strong></td>
              </tr>
              <tr>
                <td><strong>INVOICE#: {{$invoice->invoice_number}}</strong></td>
              </tr> 
              <tr>
                <td><strong>Buyer: {{$invoice->name}}</strong></td>
              </tr>
            </table>

Getting error Undefined variable $invoice

0 likes
7 replies
rodrigo.pedra's avatar

Try changing this:

$pdf = PDF::loadView('invoices.print', ['invoice' => $invoice] );
Laracast13's avatar

Problem is that when in blade

I have

stylesheet and div it not works, but if I use simple html table it generate pdf. how can i fix this ?

MichalOravec's avatar
Level 75

How do you import css? With asset()? Use public_path() instead.

Laracast13's avatar

Using


  <link rel="stylesheet" href="{{asset('dist/css/adminlte.min.css')}}">

eludic's avatar

With DomPDF I have noticed that if your CSS is extensively coded or the asset links are incorrect, the script just keeps looking and eventually times out without throwing an error. You might want to check if this is not the case in your issue.

Please or to participate in this conversation.