We would like to share with you how to create pdf and download pdf in the laravel 6 Application.
Today we will implement pdf functionality with example step by step in laravel 6 based project.
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.
We will use laravel dom-pdf package, generate and download pdf in few simple steps. Just follow the few steps and learn how to use dom-pdf in laravel.
Laravel PDF Tutorial
Contents
- Install Laravel App
- Setup Database
- Install laravel-dompdf Package
- Make Route
- Create Controller & Methods
- Create Blade View
- Start Development Server
- Conclusion
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
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
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,
]
Make Route
We will create two routes in web.php file. Go to app/routes/web.php file and create two below routes here :
Route::get('pdf_form', '[email protected]');
Route::get('pdf_download', '[email protected]');
Create Controller
We need to create a controller name PdfController. Use the below command and create Controller :
php artisan make:controller PdfController
After successfully create controller go to app/controllers/PdfController.php and put the below methods :
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Redirect; use PDF; class PdfController 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'); } }
Create Blade view
In this step 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('form-store') }}" 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>
Start Development Server
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 download tutorial, We have successfully created a PDF file and downloaded the pdf file. If you want to replace other data in your pdf file so you can change it. our examples run quickly.
If you have any questions or thoughts to share, use the comment form below to reach us.
HI
Hi too
should be in pdf_form.blad.php ?
should be public function pdfDownload(Request $request){ —– } in app/controllers/PdfController.php
Good
help where is form-store?
Cialis Aspirina Eraveden buy generic cialis online fradrady Cialis En Barcelona