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

dilfdo's avatar

how to generate PDF from blade laravel 5.1

i want to generate pdf file when user clicking id of the each row of my index view

index.blade.php

 <td style="">{{$user->id}}</td>
      <td><a href="{{url('user') . '/' . $user->id . '/view'}}"><i class="glyphicon glyphicon-th-large"></i></a></td>



and my user view function in controller

public function customerView($id) {
    $user = Festivals::where('id',$id)->first();

return view('user.view',['user'=>$user,'type'=>'Customer - View User Details']);

}

i want to add generate pdf option when user click on index page id and in specific view page both.

please advice me

i tried using dompdf , but it didnt work

    public function pdf($id)
{
      $user = Festivals::where('id',$id)->first();
       $pdf = PDF::loadView('user.view', $user);
return $pdf->download('invoice.pdf');
}
0 likes
12 replies
jlrdw's avatar

Try following the steps from their site https://github.com/dompdf/dompdf

//Just pass your HTML in to dompdf and stream the output:

// reference the Dompdf namespace
use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to Browser
$dompdf->stream();
3 likes
AnnaJeanine's avatar

Hi @dilfdo

I am having the same problem with Dompdf when passing parameters to the view which I want to generate into a pdf. Have you found a solution yet?

Cheers

mcenroe's avatar

I had a lot of problems with PDF generation in a project I've worked in the past. I finally ended up using SnappyPDF which turned out to be great. You can load views and pass variables with no issues and easily return a pdf out of it like this:

$pdf = SnappyPdf::loadView('pdf.packing-slips', $purchaseView, $addresses);
return $pdf->download($purchase['id'] . '-packing.pdf');

I highly recommend you this package :)

https://github.com/barryvdh/laravel-snappy

mcenroe's avatar

@PeterPan Yes, as he says the main difference is the rendering engine. I've tried dompdf first and when using divs I couldn't get my views to properly format (they did ok using tables). Instead, snappy did a great job and rendered the views exactly the way they should. That was my experience, it doesn't mean it necessarily has to be this way for everybody else. Let us know how it worked out for you :)

1 like
PeterPan's avatar

@mcenroe I am a newbie to laravel so i would go with the easiest route. I saw that that Snappy needs to install a binary first on the server, so i might run into problems later on when i deploy.

As of now, I am reading alot of forum questions on deploying my first ever app to the WWW.. lol

Eitherway, from the way it is described, the API command for both dompdf and snappy are the same. So when I gained experienced and confidence it the future, i can easily switch over if i see a need to.

1 like
4ndro1d's avatar

How do I embed css into my pdf shizzl? It looks just like crap

MalaniDerrick's avatar

array_ids = [1, 2, 10] // will be passed in return \App\Metrics::find($array_ids, ['column_name']);

kristjan.reinsberg's avatar
        $dompdf = new Dompdf();        
        //GET OFFER DATA
        $offer = Offer::find($request->id);
        //GET CLIENT DATA
        $client = Client::find($offer->o_client_id);
        $dompdf->loadHtml(view('pdf.offer', compact('offer', 'client')));
        $dompdf->render();
janvleugels's avatar

Does anybody know how to set an existing pdf as background to a new DOMPdf?

My company has default stationary paper. I have this in PDF. And I want that PDF (optional) as the background of my invoice.

I tried to convert the pdf to a png in 96 DPI, and set the image as background to the body of my view, but the image is nog showing correctly. I have a top border of 30 px.

Can anybody help me with this?

Please or to participate in this conversation.