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

ChiefJS's avatar

Generate PDF from current blade

I've just installed DOMPDF Wrapper for Laravel 5. I would like to generate a pdf from the current view I'm on. How do I do that?

0 likes
18 replies
bobbybouwmann's avatar

It's all in the documentation

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

return $pdf->download('invoice.pdf');
1 like
t0berius's avatar

Show more code, seems like you forgot to declare item variable.

ChiefJS's avatar

@bobbybouwmann @jaheller

$data = array([
   'invoice', 'actors', 'clients', 'items'

]);

$pdf = PDF::loadView('pdf.lpo', $data);
return $pdf->download('invoice.pdf');
bobbybouwmann's avatar

Just like your normal views you need to pass all the data to is

public function index()
{
    $users = User::all();
    $posts = Post::all();

    return view('dashboard', compact('users', 'posts'));
}

public function pdf()
{
    $users = User::all();
    $posts = Post::all();

    $pdf = PDF::loadView('dashboard', compact('users', 'posts'));

    return $pdf->download('invoice.pdf');
}
bobbybouwmann's avatar

Well look at your code! The error is can't find item while you pass items to the view!!

ChiefJS's avatar

@bobbybouwmann Would this work

public function pdf($id, $post)
{
    $users = User::where([['id', $id]])->get();
    $posts = Post::where([['id', $post]])->get();

    $pdf = PDF::loadView('dashboard', compact('users', 'posts'));

    return $pdf->download('invoice.pdf');
}
bobbybouwmann's avatar

This would work, you can even make it like so

$user = User::findOrFail($id); // This is a single user
$post = Post::findOrFail($post); // THis is a single post

$pdf = PDF::loadView('dashboard', compact('user', 'post'));
ChiefJS's avatar

@bobbybouwmann Just got an error

Missing argument 1 for App\Http\Controllers\MyController::generatePDF()

on

public function pdf($id, $post)
bobbybouwmann's avatar

Well it says that you miss an argument... I don't see your generatePDF function anywhere.. You can think this one out by yourself ;)

bobbybouwmann's avatar

You don't specify the parameters in your route so you can't expect them to appear in your controller... I think you need to learn Laravel first before you start doing these kind of things!

puzbie's avatar

I would suggest two things:

Firstly, check that you have a working view that you can see on screen. Once you know the route file, controller and view are actually working properly, then you can start worrying about PDFs.

Once you have done that, create another view, containing just the text "Hello World". Then create the route and controller to display that. Finally, try and get your PDF routine to display that.

Once you can generate a PDF containing Hellow World, then try it on your other view.

Basically, baby steps.

Now, with regards to your route. You said it was this:

Route::post('generate/pdf', 'MyController@generatePDF');

And that this calls the following function:

public function generatePDF($id, $post)

The issue here is that the router isn't being told to pass the parameters. This begs the question, how do you expect to call this route? If it is from a URL (ie http://yoursite.com/generate/pdf/id/post) then you need to amend your route to cater for this:

Route::post('generate/pdf/{id}/{post}', 'MyController@generatePDF');

However, given that you are using POST, I am thinking that id and post might be being returned from a form. In which case, they won't be parameters in generatePDF(), you will have to access them using REQUEST.

I strongly advise you follow the Introduction To Laravel course, as this will explain to you how REQUEST works, and a whole host of other things.

1 like

Please or to participate in this conversation.