【问题标题】:Laravel livewire DOM PDF the file save but not downloadedLaravel livewire DOM PDF文件保存但未下载
【发布时间】:2021-09-09 10:11:24
【问题描述】:

我正在使用DOM PDF打印html发票页面,通过livewire方法调用方法,文件正确保存到公共文件夹但我需要通过浏览器下载而不是保存它。

组件刀片视图:

<div class="m-3 ml-auto">
    <button wire:click="exportPDF()" type="button"
            class="border border-indigo-500 text-indigo-500 rounded-md px-4 py-2 m-2 transition duration-500 ease select-none hover:text-white hover:bg-indigo-600 focus:outline-none focus:shadow-outline">
            {{ __('Export PDF') }}
    </button>
</div>

Livewire 组件方法:

public function exportPDF()
{
   $order = $this->order;
   $view = view('order')->with(compact('order'));
   $html = $view->render();
   $pdf = PDF::loadHTML($html)->save(public_path() . '/order.pdf');
   
   //return $pdf->download('download.pdf');
   //    $pdf= PDF::loadHTML($html);
   //     return $pdf->download('order.pdf');
}

【问题讨论】:

  • 欢迎来到 SO @Alaa,我会尽力帮你写一个答案。

标签: php laravel dompdf laravel-livewire


【解决方案1】:

我使用这种方法来创建和下载PDF

$data = [
            'company' => $company,
            'logo' => $company->logo ? $logo : null,
            'customer' => $this->customer ?? null,
            'charged' => $charged,
            'sumChargedDuration' => $sumChargedDuration,
            'sumCharged' => $sumCharged,
            'notCharged' => $notCharged,
            'sumNotChargedDuration' => $sumNotChargedDuration,
            'sumNotCharged' => $sumNotCharged
];

$pdf = PDF::loadView('partials.pdf_generate_connections', $data)->setPaper('a4', 'landscape')->output(); //
return response()->streamDownload(
    fn() => print($pdf), 'export_protocol.pdf'
);

【讨论】:

  • Prospero,很好的答案!您是否尝试过使用 livewire 组件?
  • @ViníciusFagundes 这完全是在 Livewire 组件中...... Laravel 控制器的示例在 Livewire 上的工作方式不同,所以我找到了这个解决方案并且很受欢迎!
【解决方案2】:

最简单的答案,重定向到你的pdf文件:

$this->redirect('/<filename here>');

在你的情况下:

public function exportPDF()
{
   $order = $this->order;
   $view = view('order')->with(compact('order'));
   $html = $view->render();
   $pdf = PDF::loadHTML($html)->save(public_path() . '/order.pdf');
     
   $this->redirect('/order.pdf');
}

【讨论】:

  • @Alaa,如果我的回答对您有帮助,请考虑标记为正确并支持。
猜你喜欢
  • 2022-01-14
  • 2018-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-10
  • 2017-04-17
  • 1970-01-01
  • 2022-01-15
相关资源
最近更新 更多