如何在laravel 5.4下载PDF?

时间:2017-03-22 13:05:05

标签: laravel pdf laravel-5 laravel-5.4

我正在尝试从数据库中获取数据并将此数据传递给pdf视图并下载。

我试过这段代码,但它不起作用:

<a href="{{url('/download-PDF/'.$eventData->id)}}" class="btn btn-danger pull-right">Download PDF </a>

路线

Route::get('download-PDF/{id}', 'PDFController@pdf');

控制器

class PDFController extends Controller
{
public function pdf($id){
    $getEvent=Event::find($id);
    $eventId=$getEvent->id;


    if(isset($eventId)) {

        $eventData = Event::where('id', $eventId)->first();
        $getDays = Day::where('event_id', $eventId)->get();
        for ($i = 0; $i < count($getDays); $i++) {
            $dayId = $getDays[$i]->id;
            $schedule[$i] = DaySchedule::where('day_id', $dayId)->get();
        }

    }

    $pdf=PDF::loadView('pdf',['eventData' => $eventData, 'schedule' => $schedule]);

    return $pdf->download('event.pdf');
}
}

配置

'providers' => [
  Barryvdh\DomPDF\ServiceProvider::class,
 ]

 'aliases' => [
 'PDF' => Barryvdh\DomPDF\Facade::class,
  ]

1 个答案:

答案 0 :(得分:1)

你有任何例外吗?

这是我用来生成pdfs的代码:

<强>控制器:

'providers' => [
    ...
    Barryvdh\DomPDF\ServiceProvider::class,
    ],

'aliases' => [
    'App' => Illuminate\Support\Facades\App::class,
    ...
    'PDF' => Barryvdh\DomPDF\Facade::class,
    ]

<强>配置

Route::get('projects/{id}/report', 'Controller@generateReport');

<强>路线

class MyFormSet(BaseInlineFormSet):

    def get_form_kwargs(self, index):
        kwargs = super(MyFormSet, self).get_form_kwargs(index)
        kwargs.update({'parent': self.instance})
        return kwargs