Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

noblemfd's avatar

ERROR: Call to undefined method Barryvdh\DomPDF\PDF::with()

In my Laravel-5.8, I am trying to use Barryvdh DomPDF to export to PDF.

class AppraisalGoal extends Model
{
    protected $table = 'appraisal_goals';

    protected $fillable = [
                  'id',
                  'goal_type_id'
                  'appraisal_identity_id',
                  'employee_id',
                  'is_mid_year_published',
                  'is_visible',
                  'is_special_project',
              ]; 

    public function goaltype()
    {
        return $this->belongsTo('App\Models\Appraisal\AppraisalGoalType','goal_type_id');
    }

    public function appraisalidentity()
    {
        return $this->belongsTo('App\Models\Appraisal\AppraisalIdentity','appraisal_identity_id');
    }

    public function appraisalgoaldetail(){
        return $this->hasMany('App\Models\Appraisal\AppraisalGoalDetail');
    }

    public function company()
    {
        return $this->belongsTo('App\Models\Organization\OrgCompany','company_id');
    }       
}

Here is the controller:

public function appraisalPdf(Request $request)
{     
    $userCompany = Auth::user()->company_id;
    $userEmployee = Auth::user()->employee_id;   
    $employee = HrEmployee::where('id', $userEmployee)->first();
    $company = OrgCompany::where('id', $userCompany)->first();        
    $render=[];
    $currentappraisal = AppraisalIdentity::select('appraisal_name', 'appraisal_start', 'appraisal_end')->where('company_id', $userCompany)->where('is_current', 1)->first();
    $finalratingstatus = AppraisalParameter::select('activate_final_rating')->where('company_id', $userCompany)->first();
    $currentappraisalstatus = AppraisalIdentity::select('is_current')->where('company_id', $userCompany)->where('is_current', 1)->first();
    $goals = AppraisalGoal::where('employee_id', $userEmployee)->where(function ($query) {
                $query->where('is_approved', 3)->orWhere('is_visible', 0);
            })->whereNull('deleted_at')->orderBy('goal_type_id');  
            
    $goals=$goals->with('appraisalidentity');   
    
    if(isset($request->appraisal_identity_id))
    {
        $goals=$goals->where('appraisal_identity_id',$request->appraisal_identity_id);
        $render['appraisal_identity_id']=$request->appraisal_identity_id;
    }  

    $goals= $goals->get();
    $data['goals'] = $goals;
    $data['appraisalidentities']= AppraisalIdentity::where('company_id', $userCompany)->pluck('appraisal_name','id');          
    
    
    $pdf = PDF::loadView('appraisal-default-pdf', $data)
            ->with('employee', $employee)
            ->with('company', $company)
            ->with('finalratingstatus', $finalratingstatus)
             ->with('currentappraisalstatus', $currentappraisalstatus)
            ->with('currentappraisal', $currentappraisal);                 
    return $pdf->setPaper('a4', 'landscape')->download('AppraisalReport.pdf');        
}

route:

Route::get('/appraisal-default-pdf', 'HomeController@appraisalPdf')->name('appraisal-default-pdf');
Route::get('/appraisal-default', 'HomeController@appraisal')->name('appraisal-default');

View blade: appraisal-default

<section class="content">
    <div class="container-fluid">
    {{ Form::model(request(),['method'=>'get']) }}
    <div class="row" style="margin-bottom: 10px">

        <div class="col-sm-11">
            {{ Form::select('appraisal_identity_id',$appraisalidentities,null,['class'=>'form-control select2bs4','placeholder'=>'Select Appraisal Period']) }}
        </div>                 

        <div class="col-xs-2">
            {{ Form::submit('Search',['class'=>'btn btn-warning']) }}
        </div>

    </div>
    {{ Form::close() }}
    </div>                                               
</section>

                @foreach($goals as $key => $goal)
                        <td>
                            {{$goal->goaltype->name ?? '' }}
                        </td>                           
                        <td>
                            <b>{{$goal->goal_title ?? '' }}</b>
                        </td> 
                @endforeach 

      <div class="card-footer">
      <a href="{{ route('appraisal-default-pdf') }}" target="_blank" class="btn btn-success" style="margin-right: 5px;">
                <i class="fa fa-print"></i> Print
              </a>   
    </div>

I filter the table in the view blade with a dropdown using: $appraisalidentities. This works fine.

How when I tried to export to PDF using {{ route('appraisal-default-pdf') }}, I got this error:

ERROR: Call to undefined method Barryvdh\DomPDF\PDF::with()

How do I resolve this?

Thanks

0 likes
12 replies
noblemfd's avatar

@automica - The error is here:

$pdf = PDF::loadView('appraisal-default-pdf', $data)
        ->with('employee', $employee)
        ->with('company', $company)
        ->with('finalratingstatus', $finalratingstatus)
         ->with('currentappraisalstatus', $currentappraisalstatus)
        ->with('currentappraisal', $currentappraisal);                 
return $pdf->setPaper('a4', 'landscape')->download('AppraisalReport.pdf');

I mean ->with()

Snapey's avatar

As ever, Waaay too much information. How much of what is posted is relevant to the question?

just this;

    $pdf = PDF::loadView('appraisal-default-pdf', $data)
            ->with('employee', $employee)
            ->with('company', $company)
            ->with('finalratingstatus', $finalratingstatus)
             ->with('currentappraisalstatus', $currentappraisalstatus)
            ->with('currentappraisal', $currentappraisal);                 
    return $pdf->setPaper('a4', 'landscape')->download('AppraisalReport.pdf');    

instead, use compact

    $pdf = PDF::loadView('appraisal-default-pdf', compact(
            'data','employee','company','finalratingstatus','currentappraisalstatus', 'currentappraisal'
    ));
                 
    return $pdf->setPaper('a4', 'landscape')->download('AppraisalReport.pdf');    

noblemfd's avatar

@snapey - Thanks so much. I now got this error:

Undefined variable: goals

and it points to this line:

@foreach($goals as $goal)	

in:

appraisal-default-pdf

So sorry about posting too much things. I just wanted to give enough details

automica's avatar
automica
Best Answer
Level 54

@noblemfd is $goals in your controller method anymore?

according to your snippet it was:

    $goals= $goals->get();
    $data['goals'] = $goals;
    $data['appraisalidentities']= AppraisalIdentity::where('company_id', $userCompany)->pluck('appraisal_name','id');          
    

you don't need to define a base $data array, just add 'goals' and 'appraisalidentities'

$pdf = PDF::loadView('appraisal-default-pdf', compact(
    'appraisalidentities',
    'goals',
    'employee',
    'company',
    'finalratingstatus',
    'currentappraisalstatus',
    'currentappraisal'
));
MichalOravec's avatar

You apply nothing what we advised you in the past. So why we should waste our time by reading your issues?

Snapey's avatar
    $pdf = PDF::loadView('appraisal-default-pdf', compact(
            'data','employee','company','finalratingstatus','currentappraisalstatus', 'currentappraisal'
    ));

do you see goals being passed to the pdf?

Snapey's avatar

@automica has already answered that question, but this stupid conversation threading hides his response earlier in the thread,

Please or to participate in this conversation.