Make A Laravel Pdf Generator Using Barryvdh Dompdf - FreeCodeBlog

Last updated: Dec 12, 2021

Make a laravel pdf generator in laravel using barryvdh dompdf. You can easily generate a pdf file from a HTML file using DomPDF. To export into PDF, We need to write the HTML code in view file. Want to generate HTML file to PDF file in laravel? Actually you need to use barryvdh dompdf in laravel project. Barryvdh Dompdf can convert HTML to PDF. To convert HTML to PDF you may install barryvdh laravel dompdf. Also you may need to use ServiceProvider, facades, config dompdf and more. I hope this tutorial will help you to make a laravel pdf generator using barryvdh dompdf.

 

Make A Laravel Pdf Generator Using Barryvdh Dompdf
Make A Laravel Pdf Generator Using Barryvdh Dompdf

 

1. Install Barryvdh Dompdf.

Want to generate a PDF in laravel? Yes. Actually you can't generate a PDF without barryvdh dompdf installation. So you must need to install barryvdh dompdf to generate a PDF in laravel. You need to add  barryvdh dompdf package in your composer.json. barryvdh dompdf will download the package and the dompdf + fontlib libraries also. barryvdh dompdf support all available supported laravel So you can easily install barryvdh domPDF in laravel using composer.

 

composer require barryvdh/laravel-dompdf

 

2. Barryvdh Dompdf ServiceProvider.

Have you installed Barryvdh DomPDF in Laravel? Yes! Okay after updating composer, you need to add the ServiceProvider to the providers array in config/app.php.

 

Barryvdh\DomPDF\ServiceProvider::class,

 

3. Barryvdh Dompdf Facade.

Have you added Barryvdh DomPDF ServiceProvider  in Laravel config/app.php? Yes! Okay now you can optionally use the facade for shorter code. Add this to your facades.

 

'PDF' => Barryvdh\DomPDF\Facade::class,

 

4. Publish Barryvdh Dompdf Config File.

Have you configure Barryvdh DomPDF facade in Laravel config/app.php? Yes! Okay now it's time to publish Barryvdh DomPDF config file. Use php artisan vendor:publish to create a config file located at config/dompdf.php which will allow you to define local configurations to change some settings (default paper etc). You can also use your ConfigProvider to set certain keys. The defaults configuration settings are set in config/dompdf.php. Copy this file to your own config directory to modify the values. You can publish the config using this command.

 

php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"

 

5. Use Barryvdh Dompdf Facade.

Have you configure everything to use Barryvdh DomPDF? Maybe! First make sure that you configure everything that need to generate a PDF in laravel. You need to follow upper steps to make a PDF generator in laravel using barryvdh dompdf. Okay now it's time to use barryvdh dompdf that you added in config/app.php file. You can easily use facade from laravel controller or web route. The PDF facade load view will load laravel blade file. You can send unlimited data to laravel blade file from load view using barryvdh dompdf facade. You can also return the load view file to download.

 

$data = 'Make Laravel PDF Generator';
$pdf = PDF::loadView('pdf', compact('data'));
return $pdf->download('file.pdf');

 

6. Use Barryvdh Dompdf Stream.

You can create a new DOMPDF instance and load a HTML string, file or view name. You can save it to a file, or stream (show in browser) or download.

 

$pdf = App::make('pdf');
$pdf->loadHTML('<h1>Make A Laravel Pdf Generator Using Barryvdh Dompdf</h1>');
return $pdf->stream();

 

7. Use Barryvdh Dompdf Chain.

Want to use barryvdh dompdf chain method in laravel? Yes! Well do you know when to use the barryvdh dompdf chain method in laravel? Actually I am not sure that when I needed to use barryvdh dompdf chain method in laravel project. Okay listen, Suppose you have an invoice HTML file which is stored in a public directory, Now you want to save this HTML file to public directory as a PDF file and want to see generated PDF file at the same moment.

 

return PDF::loadFile(public_path().'/invoice.html')->save(public_path().'/invoice.pdf')->stream('invoice.pdf');

 

Okay so this is for today. See you again next tutorials. Thanks

Post Tags
Post Writer
masum7323
Masum Biswas (@masum7323)

My name is Masum Biswas. I'm a full stack developer. I live in Bangladesh and I love to write tutorials and tools that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, MySQL, VueJS, Tailwind CSS and Bootstrap from the early stage.

Follow Me On
Related Posts
Laravel Jetstream User Email Verification Full Tutorial

Laravel jetstream user email verification! really? Yes, it's true that you can easily make laravel jetstream email verification. Do you want to cre... Read More

Install Custom Design Files On Laravel

Sometimes you may want to install your designed (font-end) files bootstrap, tailwind css, custom css, custom js on laravel. so let's start to read... Read More

How To Use Vue Multiselect In Laravel With Vue Js

Sometimes you need to add vue multiselect third party package in your laravel 8 with vue 3 project. We have many third party (vue multiselect) to u... Read More

Best Cloud Hosting For Laravel Project

Laravel is a widely used and easy open source PHP framework which is used to create complex and simple web applications. It is based on a architect... Read More

How To Add Laravel Website To Google Search Console

Want to add your Laravel website to Google Search Console? Yes!, To rank Laravel website on Google, you need to add your Laravel website to Google... Read More