Laravel – Generate PDF file from view

We gonna use barryvdh/laravel-dompdf Laravel package to generate PDF files from view file. This package is just a wrapper around DOMPDF library.

Generate PDF file from view

Installtion

Use below composer command to install the package

After installing laravel-dompdf, add the ServiceProvider class to the provider’s array in config/app.php

You can optionally use the facade for shorter code. So when you need to generate PDF you just need to include it with use PDF, you dont have to metion full namesapce. Add this to your facades array in config/app.php

Great! installation and configuration part is completed. Now let use this package.

Generate a controller called UserController.php with below command

After generating controller add following index and report methods to it. Index is to show the list of users and report is to generate PDF and to download the PDF.

Create a users directory with index.blade.php and report.balde.php files.

index.blade.php

report.blade.php

Now define route to access controller methods index and report, add follwing lines to routes/web.php

That’s it. Now, with http://localhost:8000/users/report/view, you can render pdf in the browser, with http://localhost:8000/users/report/download you can download the user’s list pdf.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

Arjun

I am Arjun from Hyderabad (India). I have been working as a software engineer from last 7+ years, and its my passion to learn new things and implement them as a practice. Aside from work, I likes gardening and spending time with pets.


DMCA.com Protection Status