Generate pdf in laravel 7.x/6.x. Here, we would like to share with you how to create pdf and download pdf in the laravel 7/6 Application.
Most of the E-commerce projects, Shopping websites, E-Marketing Websites have built-in laravel. These types of websites generate invoices, acknowledgment, tickets and many more things. In this case, we use the laravel dom-pdf packages and easily create invoices, acknowledgments, tickets, etc.
Generate or create PDF in laravel 7/6, we will use laravel dom-pdf package to generate/create and download pdf.
Generate/Create PDF in Laravel 7/6
Use the below given steps to generate or create pdf in laravel. As well as download pdf in laravel:
- Install Laravel App
- Setup Database
- Install laravel-dompdf Package
- Make PDF Route
- Create GeneratePDF Controller
- Create Blade view for Generate Pdf
- Start Development Server
1. Install Laravel App
First, we need to download the laravel fresh setup. Use the below command and download fresh new laravel setup :
composer create-project --prefer-dist laravel/laravel blog
2. Setup Database
After successfully download laravel Application, Go to your project .env file and set up database credential and move next step :
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=here your database name here
DB_USERNAME=here database username here
DB_PASSWORD=here database password here
3. Install laravel-dompdf Package
Now, we need to download the laravel-dompdf package, it package help to create and download pdf. Laravel dompdf pacakge has convert blade view to pdf. Use the below command and download laravel dompdf :
composer require barryvdh/laravel-dompdf
After successfully install the laravel dompdf package, open the config/app.php and config the providers and aliases :
'providers' => [
Barryvdh\DomPDF\ServiceProvider::class,
],
'aliases' => [
'PDF' => Barryvdh\DomPDF\Facade::class,
]
4. Make PDF Route
Now, We will create two routes in web.php file. Go to /routes/web.php file and create two below routes here :
Route::get('pdf_form', 'GeneratePdfController@pdfForm'); Route::get('pdf_download', 'GeneratePdfController@pdfDownload');
5. Create GeneratePDF Controller
Here, we need to create a controller name GeneratePdfController. Use the below command and create Controller :
php artisan make:controller GeneratePdfController
After successfully create controller go to app/controllers/GeneratePdfController.php and put the below methods :
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Redirect; use PDF; class GeneratePdfController extends Controller { public function pdfForm() { return view('pdf_form'); } public function pdfDownload(){ request()->validate([ 'name' => 'required', 'email' => 'required|email', 'message' => 'required' ]); $data = [ 'name' => $request->name, 'email' => $request->email, 'message' => $request->message ]; $pdf = PDF::loadView('pdf_download', $data); return $pdf->download('tutsmake.pdf'); } }
6. Create Blade view for Generate Pdf
Next, we need to create blade view file. Go to app/resources/views and create one file name pdf_form.blade.php :
<html> <head> <title>Laravel Generate & Download Pdf Tutorial</title> <link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script> </head> <style type="text/css"> body{ background-color: #f1f1f1; } </style> <body> <div class="container contact"> <br><br><br> <div class="row"> <div class="col-md-8 offset-md-2"> <form action="{{ url('pdf_download') }}" method="post" accept-charset="utf-8"> @csrf <div class="contact-form"> <div class="form-group"> <label class="control-label col-sm-2" for="fname">First Name:</label> <div class="col-sm-10"> <input type="text" class="form-control" id="name" placeholder="Enter Name" name="name"> <span class="text-danger">{{ $errors->first('name') }}</span> </div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="email">Email:</label> <div class="col-sm-10"> <input type="email" class="form-control" id="email" placeholder="Enter email" name="email"> <span class="text-danger">{{ $errors->first('email') }}</span> </div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="comment">Comment:</label> <div class="col-sm-10"> <textarea class="form-control" rows="5" name="message" id="message"></textarea> <span class="text-danger">{{ $errors->first('message') }}</span> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">Submit</button> </div> </div> </div> </form> </div> </div> <br><br><br><br> </div> </body> </html>
Now we create a one more blade view file name pdf_download.blade.php. Go to app/resources/views create file. This blade view file will download as pdf file. so put the below code here :
<body> <div id="content"> <h2>Hello <b> <span style="color:red">{{ ucfirst($name) }}</span> </b></h2> <p> {{ $message }} </p> </div> </body>
7. Run Development Server
Now, we need to start development server. Use the php artisan serve command and start your server :
php artisan serve
If you want to run the project diffrent port so use this below command
php artisan serve --port=8080
Now we are ready to run our example so run bellow command to quick run.
http://localhost:8000/pdf
Conclusion
In this laravel pdf generate and download tutorial, you have successfully created a PDF file and downloaded the pdf file in laravel7/6.
Note that, If you want to replace other data in your pdf file so you can change it. our examples run quickly.
Recommended Laravel Posts
If you have any questions or thoughts to share, use the comment form below to reach us.