Laravel Generate PDF And Send Email Example

Hello Artisan

In this brand new tutorial i am going to discuss about Laravel generate pdf and send email. In this tutorial i will generate pdf and then send it to user via mail in laravel. If you don;t know how to generate pdf then this tutorial is for you. 

In this tutorial i will also discuss about how to get data between two dates in Laravel. So after finishing this project you will also learn where between laravel query. We will find or fetch data between two dates using whereBetween. 

In this tutorial i will simple create some users and then i will create a search html form. Using this form i will put two date field. One is start date and other is end date. Between those date i will fetch all the data from database. After fetch i will generate pdf and then i will send this pdf to admin via email in laravel.

Hope you will enjoy this Laravel generate pdf and send email. In this tutorial. Take a look on those below image of preview of this tutorial.


Preview : Form for searching user




Preview : Our table data




Preview : After downloading PDF



Preview : After Sending Mail



Step 1: Download Laravel

To build fetch data between two dates in Laravel download a fresh laravel project by the following command.

composer create-project --prefer-dist laravel/laravel blog


Step 2: Add Route

Now we need routes to see our input page where i will give two date input. So create this below route and create some dummy data of users.


Route::get('/', 'TestController@index');
Route::get('/report', 'TestController@daily_report')->name('report');


Step 3: Add Controller 

In this step first you have to install PDF package to generate PDF. In this tutorial i will use /laravel-pdf pckage to generate pdf. So run following command to install it.

composer require niklasravnsborg/laravel-pdf


To start using Laravel, add the Service Provider and the Facade to your config/app.php:


'providers' => [
	// ...
'aliases' => [
	// ...
	'PDF' => niklasravnsborg\LaravelPdf\Facades\Pdf::class



Now, you should publish package's config file to your config directory by using following command:

php artisan vendor:publish


Ok, now we have to add new controller method "index()"  and daily_report() in your TestController, so if you haven't created TestController then you can create it and paste this following code.


namespace App\Http\Controllers;

use App\Mail\CheckUser;
use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use PDF;

class TestController extends Controller
    public function index()
       return view('welcome');

    public function daily_report(Request $request)
       $start_date = Carbon::parse($request->start_date)

       $end_date = Carbon::parse($request->end_date)

       $data['users'] = User::whereBetween('created_at',[$start_date,$end_date])->get();
       $data['start_date'] = Carbon::parse($request->start_date)
       $data['end_date'] = Carbon::parse($request->end_date)
       $count = User::whereBetween('created_at',[$start_date,$end_date])->count();

       if( $count < 1 ) {
        session()->flash('message','There is no user between those date!');
        return redirect()->back();

       $pdf = PDF::loadView('test', $data, [
                   'format' => 'A4'

        \Mail::send('test', $data, function($message) use ($pdf){
            $message->subject('Date wise user report');

       $pdf->SetProtection(['copy', 'print'], '', 'pass');
       return $pdf->stream('document.pdf'); 



Step 4: Create View 

In this step we need to create to blade file. One is for form and other is our generated pdf file. So create it.



Read also : Laravel 7.x Queues Example with Redis and Horizon




Read also : How to Generate HTML to PDF Using Laravel Dompdf


In this tutorial i will generate pdf and send it to user via email in laravel. I will also showed how we can fetch database data between two dates. Hope it can help you.


#laravel #laravel-7 #laravel-7x #email #pdf #html-to-pdf #laravel-carbon #tutorial