7

I am using dompdf in my Laravel v8.26.1 app. I am also using Laravel Livewire v1.3.5.

I can get a view to download a pdf outside of Livewire, but can't seem to get it to work inside a Livewire view. I've tried every configuration I could think of, but keep running into road blocks. I am either getting an Undefined variable: workorder error, or no error at all, but also no download.

route

...
Route::livewire('/workorder/{workorder}/print', 'admin.workorders.printer')->name('admin.workorder.print')->layout('layouts.admin');
...

livewire/admin/workorders/print.blade

...
<div>
  <button wire:click="generatePDF({{$workorder->id}})">Create PDF</button>
</div>
...

livewire/admin/workorders/printer.php

...
use Barryvdh\DomPDF\Facade as PDF;
...
public function generatePDF($id)
{
    $record = Workorder::find($id);
    $data = [
        'project_id' => $record->project_id,
        'project_name' => $record->project_name,
        'customer_id' => $record->customer_id,
        'generator_id' => $record->generator_id,
        'workorder_po_number' => $record->workorder_po_number,
        'workorder_number' => $record->workorder_number,
        'prevailing_wage' => $record->prevailing_wage,
    ];
    $pdf = PDF::loadView('livewire.admin.workorders.print', $data);
    return $pdf->download('demo.pdf');
}
1
  • did you happen to figure out how do accomplish this yet? I'm running into the same question on a current project. Commented Feb 8, 2021 at 17:57

1 Answer 1

20

Here you go, just found this. fixed me right up. The only thing I had to change was in the $viewData variable, i just had to make it an array - ['viewData' => $viewData] and then i could access it in my view.

Here is the code in case that link ever goes down:

$pdfContent = PDF::loadView('view', $viewData)->output();
return response()->streamDownload(
     fn () => print($pdfContent),
     "filename.pdf"
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.