Laravel livewire DOM PDF 文件保存但未下载

问题描述 投票:0回答:3

我正在使用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');
}
php laravel dompdf laravel-livewire
3个回答
2
投票

我用这个方法来创建和下载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'
);

1
投票

最简单的答案,重定向到您的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');
}

0
投票

问题陈述:我生成了pdf文件并上传到s3存储桶。但我无法下载该文件,但我给出了正确的路径。当我点击 url 时,它会下载文件,但我单击 livewire laravel 中的下载按钮,它不会下载该文件。

解决方案: 您将如何解决这个问题。

Step 1: Added this index in filesystem under config folder

  'pdf' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
            'endpoint' => env('AWS_ENDPOINT'),
            'visibility' => 'public',
            'root' => 'pdf',
            'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
        ],

Step 2 :  Add this in your downloading function
        return Storage::disk('pdf')->download($model_object->file_name);

Step 3: Add this namespace to your file. 
        use Illuminate\Support\Facades\Storage;
© www.soinside.com 2019 - 2024. All rights reserved.