In this tutorial you will learn about the Laravel 7/6 Generate PDF Example and its application with practical example.
In this Laravel 7/6 Generate PDF Example tutorial, I will show you how to generate pdf from html blade view file in laravel. In this tutorial you will learn to generate, convert and download pdf file from html blade view file in laravel. While working with web application we come to situations where we need to generate or download pdf file for various purpose such as generate invoices, acknowledgment or tickets printing etc. In this tutorial we will be using laravel dom-pdf package to generate/create and download pdf.
Laravel 7/6 Generate PDF Example
In this step by step guide I will demonstrate you how to create or download pdf from laravel html blade view file in the laravel 7/6 Application.
- 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 of all we need to create a fresh laravel project, download and install Laravel using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
2. Setup Database
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
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
In this step, we will install laravel-dompdf Package via the composer dependency manager. Use the following command to install laravel-dompdf Package.
1 |
composer require barryvdh/laravel-dompdf |
After successfully installing laravel dompdf package, we need to add service provider and alias in config/app.php file as following.
1 2 3 4 5 6 7 8 9 |
'providers' => [ Barryvdh\DomPDF\ServiceProvider::class, ], 'aliases' => [ 'PDF' => Barryvdh\DomPDF\Facade::class, ] |
4. Make PDF Route
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 |
Route::get('pdf_form', 'GeneratePdfController@pdfForm'); Route::get('pdf_download', 'GeneratePdfController@pdfDownload'); |
5. Create GeneratePDF Controller
Now, lets create a controller named GeneratePdfController using command given below –
1 |
php artisan make:controller GeneratePdfController |
After creating controller go to app/controllers/GeneratePdfController.php and put the following code in it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?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('w3adda.pdf'); } } |
6. Create Blade view for Generate Pdf
Now, we need to create blade view file. Go to app/resources/views and create one file name pdf_form.blade.php :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<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
1 2 3 4 5 6 7 8 |
<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 are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/pdf |