我在我的Laravel v8.26.1应用程序中使用dompdf。我也使用Laravel Livewire v1.3.5。
我可以得到一个视图来下载一个pdf在Livewire之外,但似乎不能让它在Livewire视图内工作。我尝试了我能想到的所有配置,但总是遇到障碍。我要么得到Undefined variable: workorder
错误,要么根本没有错误,但也没有下载。
...
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 BarryvdhDomPDFFacade 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');
}
给你,刚找到这个。把我治好了。我唯一要改变的是在$viewData
变量中,我只需要使它成为一个数组-['viewData' => $viewData]
,然后我可以在我的视图中访问它。
下面是以防链接断开的代码:
$pdfContent = PDF::loadView('view', $viewData)->output();
return response()->streamDownload(
fn () => print($pdfContent),
"filename.pdf"
);