【发布时间】:2022-01-14 15:39:07
【问题描述】:
在我的页面上,我正在制作与 Livewire 完全兼容的发票。我使用这个包:https://github.com/LaravelDaily/laravel-invoices 来生成我的发票,一切正常。但他们是我遇到的一个问题。我无法使用 Livewire 下载我的 PDF。
这是一个生成 PDF 并下载的基本示例:
public function invoice()
{
$customer = new Buyer([
'name' => 'John Doe',
'custom_fields' => [
'email' => 'test@example.com',
],
]);
$item = (new InvoiceItem())->title('Service 1')->pricePerUnit(2);
$invoice = Invoice::make()
->buyer($customer)
->discountByPercent(10)
->taxRate(15)
->shipping(1.99)
->addItem($item);
return $invoice->download();
}
每当我点击一个按钮时
<a role="button" class="pdf-download cursor-pointer" wire:click="invoice">download</a>
什么都没有发生。所以问题是 Livewire 不支持这种下载方式。而这个下载方式是这样的:
public function download()
{
$this->render();
return new Response($this->output, Response::HTTP_OK, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="' . $this->filename . '"',
'Content-Length' => strlen($this->output),
]);
}
$this->render(); 在特定文件夹中呈现模板
他们可以解决这个问题吗?我可以在哪里下载带有模板或不同策略的 pdf。我已经尝试过一件事。我将发票存储到会话中,如下所示:
Session::put('invoice', $invoice);
Session::save();
我有一个不同的控制器。
if ($invoice = Session::get('invoice')) {
$invoice->download();
}
但这给了我这个错误:
不允许序列化“闭包”
我尝试了一些我在这里找到的东西:https://github.com/livewire/livewire/issues/483 但没有任何效果。有人可以告诉我在哪里看或如何解决这个问题吗?谢谢!
【问题讨论】:
-
这里
if ($invoice = Session::get('invoice')) { $invoice->download(); }测试你应该使用==而不是=。 -
这里是如何实现的,我的意思是下载发票invoice download stream
-
@Prospero 哇,确实有效!谢谢:)
标签: php laravel pdf download laravel-livewire