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

Sofia's avatar
Level 6

Generating PDFs from Laravel Views

Hi,

I've been playing around with Snappy to generate PDFs from a Laravel View. The PDF is an invoice and ideally I'd like to store it and then automatically email a copy to a client.

It seems like an image of my view is being generated and I can't copy/paste text from the PDF. The files can become large if my invoices are several pages - not ideal for storage or email.

Neither of these problems occur when I print the page to a PDF using the browser (I can't remember if I have some sort of plug-in installed), so I know it's possible to generate a different type of PDF.

Any suggestions? Is there some Snappy configuration that I'm missing? Do you guys have other package suggestions?

Thanks!

0 likes
17 replies
jlrdw's avatar

If size is a problen, have you considered zipping it? Zip and un-zipping is pretty standard. I send several monthly reports via email zipped. Sorry, not in laravel though.

Sofia's avatar
Level 6

@jlrdw size is one of the problems, but zipping won't be enough. Being able to select the text is equally as important.

jlrdw's avatar

I thought it went without saying, the user could just unzip at their end. But users, we all know. Have you looked at maybe a text format? Might be a lot smaller. Just attach a text file instead of pdf. Just a thought, but interesting problem.

Sofia's avatar
Level 6

@jlrdw - text files for invoices? yikes!

@CodebyRay - Thanks! Yes, the dpi would help with the size but I still can't search and select text from the file (they way I can if I just print to pdf from my browser)

Will get back to this problem shortly and update when I come up with the right solution.

1 like
RayC's avatar

@Sofia This package will generate a PDF file and you would be able to copy and paste contents in the generated PDF file.

I have used it to generate invoices from views and it has worked very well. Works good for combining PDFfiles also.

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

Hope this helps.

Sofia's avatar
Level 6

@harjovica - Thanks, but at first glance I'm not sure that either of those are what I'm looking for....I need to convert an HTML page to a non-image PDF, not build up a PDF form using PHP. Either way, I'll take a closer look.

@CodebyRay - I started trying to use it (before Snappy), but I couldn't get the dependencies sorted out. My server's PHP is @ 5.4 and there were some packages that dompdf required that I wasn't able to get.

A php update is not up to me at this point, but I'll certainly give it another try if we upgrade or if anyone can suggest a workaround.

Good to know that it'll work for my purpose. Thanks!

36864's avatar

What version of laravel are you using? Have you looked at previous versions of laravel-dompdf to find a release that would work with your versions?

Version 0.1.0 for laravel 4 required php 5.3, so you might be able to find a release that fits your versions.

Sofia's avatar
Level 6

@36864 - Laravel 5.0. I remember playing around with different versions but a good idea to go back and take a closer look. Not sure I want to revert to Laravel 4.

Sofia's avatar
Level 6

Thanks @36864 (and @CodebyRay) for the forward nudges!

Installation of v.0.6.1 was OK (Yay!), but having some challenges with rendering the PDFs.

Will report back when I can dedicate some time to investigating the issues.

Should work with Vue, right? Update: Doesn't look like it :(

sghimire's avatar

Hi Guys,

Is there anyway generated PDF file(from a Laravel View) can be saved in the database?

sghimire's avatar

That's what I am doing right now. I want to save it to the database.

view()->share('lab',$data);
    $pdf = PDF::loadView('lab.test');
    return $pdf->download('test_'. $request['Id'] . '_lab.pdf');
Hondaman900's avatar

@HAJROVICA - @hajrovica, can you share how you got php-pdftk to install in Laravel? I installed it using composer, and it shows up under vendors, but my controller is not recognizing it, saying "Class 'Vendor\mikehaertl\pdftk\Pdf' not found". I'm using Laravel 5.5 and have installed PDftk on my system as required. Not sure what I'm missing.

Can you please help?

hajrovica's avatar

@hondaman900 Hi as i recall nothing specific was needed to install php-pdftk Pulled it with composer

used it in standard way: use mikehaertl\pdftk\Pdf;

can you share your code? also do composer selfupdate Check is your name space ok - i am not sure should it say Class 'Vendor\mikehaertl\pdftk\Pdf' not found or should it be Class 'mikehaertl\pdftk\Pdf' not found?

vajid's avatar

@sofia why dont just generate pdf on the fly and mail it to customer without being downloaded it on server.

I did similar for invoice, fetch the invoice data from db pass it to view and generate pdf with ( Images) and mail it to customer.

No need to save it no server

Please or to participate in this conversation.