Convertise Data in Pdf in laravel 5.3

2019-09-20 00:54发布

问题:

I want to convert data in pdf using laravel 5.3. I have tried using fpdf. but i did not get any success. how to convert data in pdf using laravel 5.3.

回答1:

This is the code which which I used in my ReportsController and will help you to convert your data into pdf

public function getDownloadPDF($id){

        $objReports=Walkthrough::find($id);
        $objReports=DB::table('walkthroughs')
                    ->join('users','users.id','=','walkthroughs.user_id')
                    ->select('users.first_name as FirstName','users.last_name as LastName','walkthroughs.start_date as StartDate','walkthroughs.end_date as EndDate','walkthroughs.unit as Unit','walkthroughs.reminder_date as ReminderDate','walkthroughs.address')
                    ->where('walkthroughs.id','=',$id)
                    ->first();

         $view = \View::make('pdf.admin_reports',
         compact('objReports'))->render(); 

        $pdf = \App::make('dompdf.wrapper');
        $pdf->loadHTML($view);
        $pdf_name = date('YmdHis').$id. '.pdf';
        $pdf->save(public_path('assets/pdf/' . $pdf_name));

         return Redirect::to('/assets/pdf/'.$pdf_name);
}

So similarly you can take data from database and convert it to pdf. Also use following code to render your view

$view = \View::make('pdf.walkthroughDetail',
                            compact('walkthrough','completed_time','selectedRoomTags',
                            'items','selectedTags','housemates','landlords',
                            'inspector_info','completed_time'))->render(); 

        $pdf = \App::make('dompdf.wrapper');
        $pdf->loadHTML($view);
        $pdf_name = date('Ymdhis') . '.pdf';
        $pdf->save(public_path('assets/pdf/' . $pdf_name));

In this case your view pdf.walkthroughDetail is converted into pdf file.