dompdf 未从 Laravel 8 和 Livewire 视图下载 PDF
dompdf not downloading PDF from Laravel 8 and a Livewire View
我在我的 Laravel v8.26.1 应用程序中使用 dompdf。我也在使用 Laravel Livewire v1.3.5.
我可以获得在 Livewire 外部下载 pdf 的视图,但似乎无法在 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 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');
}
Here you go,刚找到这个。把我修好。我唯一需要更改的是 $viewData
变量,我只需要将其设为数组 - ['viewData' => $viewData]
然后我就可以在我的视图中访问它。
这里是代码以防 link 出现故障:
$pdfContent = PDF::loadView('view', $viewData)->output();
return response()->streamDownload(
fn () => print($pdfContent),
"filename.pdf"
);
我在我的 Laravel v8.26.1 应用程序中使用 dompdf。我也在使用 Laravel Livewire v1.3.5.
我可以获得在 Livewire 外部下载 pdf 的视图,但似乎无法在 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 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');
}
Here you go,刚找到这个。把我修好。我唯一需要更改的是 $viewData
变量,我只需要将其设为数组 - ['viewData' => $viewData]
然后我就可以在我的视图中访问它。
这里是代码以防 link 出现故障:
$pdfContent = PDF::loadView('view', $viewData)->output();
return response()->streamDownload(
fn () => print($pdfContent),
"filename.pdf"
);