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

jenya's avatar
Level 2

Can't pass variable to a Dompdf loadView method

Hello, Laracasts. I use dompdf for Laravel, version is 0.8.2, Laravel version is 5.6.33. And i try to load my blade view with some data into .pdf file using dompdf package. Laravel view is obtained from controller method

use App\Card; 
use PDF;
............
public function show($id)
    {
      $card = Card->where('id',$id)->get(); //or another model query
      return view('print.print', ['card' => $card]);
    }

so, it's return blade view with data correclty like this

{-- print.print view--}
@foreach ($card as $value)
        <div class="row">
         <div class="col-sm-7"> 
           <h4>{{$value->Number}}</h4>
         </div>
        </div>
       <div class="row">
            <div class="col-sm-12"> 
               <b>{{$value->City}}</b>
           </div>
       </div>
      {-- and so on--}
@endforeach

Trying to load view into pdf file:

public function print_pdf()
    {    
        $pdf = PDF::loadView('print.print',array('data' =>$card));
        return $pdf->download('card.pdf');
    }

but error occurs "Undefined variable: card". Please help. Thanks!

0 likes
4 replies
Cronix's avatar

Probably because you named the variable $data instead of $card, whereas it was named $card where you said it was working (without rendering the pdf)

$pdf = PDF::loadView('print.print',array('card' =>$card));  // name the variable card

You'll also need to retrieve $card from the db like you do in your first code snippet, in order to pass it to the view.

public function print_pdf()
{
    $card = Card::all(); // you're not passing an $id to print_pdf() like you are the show() method
    $pdf = PDF::loadView('print.print', ['card' => $card]);
    return $pdf->download('card.pdf');
}
1 like
jenya's avatar
Level 2

@Cronix, thank you for your answer! I try to do

use App\Card; 
use PDF;
............
public function show($id)
    {
      $card = Card->where('id',$id)->get(); //or another model query
       $this->print_pdf($card); 
    }
//and call print_pdf method with $card parameter
public function print_pdf($card)
{
   $pdf = PDF::loadView('print.print', ['card' => $card]);
    return $pdf->download('card.pdf');
}

dd($pdf) returns some array but if i use

   $pdf = PDF::loadView('print.print', ['card' => $card]);
    return $pdf->download('card.pdf');

error occurs: Maximum execution time of 30 seconds exceeded in vendor\dompdf\dompdf\src\Css\Stylesheet.php file (i want to display in pdf not so much data, only a few rows)

migsAV's avatar

Try changing

$card = Card->where('id',$id)->get(); //or another model query

to

$card = Card::where('id',$id)->get(); //or another model query

And also try using

 $pdf = PDF::loadView('print.print', compact('card'));
BeanBoe's avatar

this worked for me $pdf = PDF: :loadView('print. print', compact('card'));

Please or to participate in this conversation.