Generate PDF From HTML in Laravel 5.7 - Harikrishnanr's Blog - Nomad PHP
Generate PDF From HTML in Laravel 5.7 - Harikrishnanr's Blog - Nomad PHP
# (https://www.linkedin.com/showcase/nomadphp/) $ (https://www.youtube.com/channel/UCYHDBrzJ5o5d97KlCAUhICA)
SPONSORS
Today, I will share with you how to create a PDF file from HTML blade file in Laravel 5.7. We will be using dompdf package for generating the
PDF file.
In the below example, we will install barryvdh/laravel-dompdf using composer package and thereafter we will add new route url with
controller. Then we will create a blade file. Then after we have to just run project with serve and we can check the PDF file is for download.
Download Laravel 5.7 Now I am going to explain the step by step from scratch with laravel installation for dompdf. To get started, we need to
download fresh Laravel 5.7 application using command, so open our terminal and run the below command in the command prompt:
composer create-project --prefer-dist laravel/laravel blog Install laravel-dompdf Package Now we will install barryvdh/laravel-dompdf composer
package by using the following composer command in ourLlaravel 5.7 application.
composer require barryvdh/laravel-dompdf Then the package is successfully installed in our application, after that open config/app.php file
and we need to add alias and service provider.
config/app.php
'aliases' => [ .... 'PDF' => Barryvdh\DomPDF\Facade::class, ] Create Routes Now we need to create routes for the items listing. so now open our
"routes/web.php" file and we need to add following route.
routes/web.php
app/Http/Controllers/HomeController.php
class HomeController extends Controller { public function demoGeneratePDF() { $data = ['title' => 'Welcome to My Blog']; $pdf =
PDF::loadView('myPDF', $data);
return $pdf->download('demo.pdf'); } } Create Blade File In the final step, let us create demoPDF.blade.php in the
resources/views/demoPDF.blade.php for structure of pdf file and add the following code:
resources/views/demoPDF.blade.php
<!DOCTYPE html> <html> <head> <title>Hi</title> </head> <body> <h1>Welcome to My BLOG - {{ $title }}</h1> <p>Lorem ipsum dolor sit amet,
consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p> </body> </html> Now run the below command for serve and test it:
SPONSORS
Information
FAQ (/static/faq)
About Us (/static/about)
Privacy Policy (/static/privacy)
Terms and Conditions (/static/terms)
Work With Us
For Teams (/teams)
For Universities (/universities)
For Webmasters (/static/webmasters)
Advertise with us (/static/advertise)