Mihiran Chathuranga
Mihiran Chathuranga

Reputation: 116

Error with barryvadh laravel dom-pdf when try to create pdf from blade.php file

I am using barryvdh's library DOMPdf to generate pdf documents from html.code of the in my reports.blade.php as shown belows.i want to create a pdf using reports.blade.php.when i click download pdf link it gives error saying undifined "user " variable.

<a href="{{url('/pdfs')}}">Download pdf</a>

@foreach ($users as $user) 

<font size="6"> 
 <ol> {{$user->name}}
    {{$user->lname}}</font><br>

@endforeach

but i run this laravel project it gives a error calling undifened variable.

codes of the reportGenerator controller as shown below.

<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\User;
use PDF;

class pdfGenerator extends Controller
{
    public function reportPdf(){
   $pdf  = PDF::loadview('reports');
   return $pdf->download('report.pdf');
   }


  public function report()
    {
        $users = DB::table('users')->get();

        return view('reports', ['users' => $users]);
    }
}

why there is a error occur calling undefined variable. but without following code part i can download empty pdf.

@foreach ($users as $user) 

<font size="6"> 
 <ol> {{$user->name}}
    {{$user->lname}}</font><br>

@endforeach

routes which i used as below

Route::get('/pdfs','pdfGenerator@reportPdf');

Route::get('/reports','pdfGenerator@report');

Upvotes: 1

Views: 1400

Answers (1)

Sohel0415
Sohel0415

Reputation: 9853

pass the $users variable to view as a second parameter.

public function reportPdf()
{
   $users = DB::table('users')->get();
   $pdf  = PDF::loadview('reports', ['users' => $users]); 


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

Upvotes: 2

Related Questions