在浏览器中定向到白页,而不是在laravel-dompdf中下载PDF

时间:2019-02-19 16:16:32

标签: dompdf laravel-5.7

我在laravel-dompdf中有问题。我认为我的代码是正确的,我不知道为什么将其定向到白页而不是下载pdf。它可以在其他控制器上工作,但这里不能。

white page

我在控制器中的代码:

public function export_pdf()
{
    if(Auth::user()->campus_id == 0){
        $processes = Process::join('bags','processes.bag_id', '=', 'bags.id')
              ->join('stations','processes.station_id', '=', 'stations.id')
              ->select('bags.*','stations.*','processes.*')
              ->where('stations.campus_id', Session::get('test'))
              ->orderBy('processes.id', 'desc')
              ->get();


        // Send data to the view using loadView function of PDF facade
        $pdf = PDF::loadView('collectionReportspdf', compact('processes'));
        // If you want to store the generated pdf to the server then you can use the store function
        $pdf->save(storage_path().'_filename.pdf');
        // Finally, you can download the file using download function
        return $pdf->download('reports.pdf');
   }
   else{
       $processes = Process::join('bags','processes.bag_id', '=', 'bags.id')
              ->join('stations','processes.station_id', '=', 'stations.id')
              ->select('bags.*','stations.*','processes.*')
              ->where('stations.campus_id', Auth::user()->campus_id)
              ->orderBy('processes.id', 'desc')
              ->get();


       // Send data to the view using loadView function of PDF facade
       $pdf = PDF::loadView('collectionReportspdf', compact('processes'));
       // If you want to store the generated pdf to the server then you can use the store function
       $pdf->save(storage_path().'_filename.pdf');
       // Finally, you can download the file using download function
       return $pdf->download('reports.pdf');
   }

}

1 个答案:

答案 0 :(得分:0)

过去,在保存或输出到浏览器之前,我不得不渲染PDF。

尝试在$pdf->save()上方添加以下内容:

$pdf->render();

如果仍然无法正常运行,请尝试将return语句替换为echo