John Davidson

php - dompdf not downloading PDF from Laravel 8 and a Livewire View

0 comments
Message:


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


My Answer:
_controller_..._public function render(Workorder $workorder)
{
$pdf = PDF::loadView('admin.workorders.print', compact('workorder'));

return $pdf->download('workorder.pdf');
}_view_..._


_component_..._public function print()
{
return redirect()->route('admin.workorders.print', $this->workorder);
}_In this code snippet, I am assuming that you have a Livewire component called `Printer` and a Livewire view called `printer.blade.php`. The Livewire component has a method called `print` which redirects to the route that generates the PDF. The route calls the `render` method in the controller, which loads the view and generates the PDF for download._Make sure that you have the necessary dependencies installed for dompdf and that your Livewire component is set up correctly. Also, ensure that the `workorder` variable is properly passed to the Livewire component._If you are still facing issues, try debugging by checking if the `workorder` variable is being passed correctly to the Livewire component and if the PDF generation is working as expected.

Rate this post

3 of 5 based on 2813 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us