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

profemzy's avatar

Barryvdh DOMPDF Usage Issues

I'm trying to use the above wrapper for PDF printing as below:

My Controller:

public function index() { $profile = Auth::user()->profile; if($profile){

        $data = array(
            'surname' =>  $profile->surname,
            'other_names' => $profile->other_names,
            'residential_add' => $profile->residential_add
        );

      $pdf = PDF::loadView('data.pdf', $data);

       return view('profiles.index', ['profile' => $profile, 'pdf' => $pdf]);

}

How do I Output the generated pdf to my view?

I keep getting error:

View [data.pdf] not found.

0 likes
11 replies
skliche's avatar
skliche
Best Answer
Level 42

@profemzy The first argument needs to be the name of an existing view file.

For example to create an invoice you have the following view (resources/views/pdf/invoice.blade.php) that serves as the template for the invoices:

<h1>Invoice</h1>
<p>{ { $name } } { { $amount } }</p>

In your controller you could create a new invoice PDF like this even though the controller might not be the past place to do this:

    $data = [
        'name' => 'profemzy',
        'amount' => 1000,
    ];
    $pdf = \PDF::loadView( 'pdf.invoice', $data );
    $path = base_path() . '/public/invoice.pdf'; // not a good idea, just a simple example
    $pdf->save( $path );

    return view( 'profiles.index', compact( 'path' ) );
1 like
profemzy's avatar

@skliche Thanks for the accurate feedback. since the controller is not the best place to create new invoice PDF is it better to do so in the view?

skliche's avatar

@profemzy No, definitely not a view. If it is just as simple as in the example above then it's pretty much ok to do it right there in the controller. You can still move it somewhere else when your requirements change. I'd use a model to create the PDF if more complex logic needs to be applied or the logic is also applied somewhere else.

profemzy's avatar

@skliche Thanks for taking time to answer my questions, one final question pls, i am trying to make the pdf pop up in the browser instead of having it save in a folder i tried this: $pdf->stream();

but still no pop up.

skliche's avatar

@profemzy What do you mean by "pop up"? If you want to automatically initiate a download, use the following:

return $pdf->download( 'invoice.pdf' );

instead of

return view( 'profiles.index', compact( 'path' ) );

Replace invoice.pdf with whatever filename is suitable for you.

If you want the file to be displayed in the web browser instead of the download use:

return $pdf->stream();

instead of

return view( 'profiles.index', compact( 'path' ) );
sunny's avatar

@skliche $pdf->stream(); does not show pdf on my chrome browser. Chrome force to download it. whats the problem you think, is it? any problem I face if i use javascript function window.print() to print & create pdf ( chrome browser support to save file as pdf )

skliche's avatar

@sunny Is the Chrome PDF Viewer disabled? Check with chrome://plugins/

skliche's avatar

@sunny Check the HTTP headers. The request should return the following HTTP headers:

Content-Disposition: inline; filename="invoice.pdf"
Content-Type: application/pdf

If these headers are present then Chrome should display the PDF instead of downloading it. Works fine in my Chrome browser as long as the PDF Viewer is enabled.

sunny's avatar

@skliche problem solved. The main problem was Internet Download Manger ( IDM ). Its interrupted request and force to download PDF file. After uninstall IDM everything find as you said. thanks bro

Please or to participate in this conversation.