Laravel 8 Generate PDF From Views Using DomPDF

Laravel

23

Admin

19-11-2020


Laravel 8 generate pdf from views, html, blade. In this tutorial, i will show you how to generate or create pdf from views, html, blade in laravel 8 app using a dom pdf package.

In this tutorial, you will learn step by step how to generate or create pdf from views, html and blade file.

Laravel 8 Create PDF From Views Tutorial

  • Install Laravel 8 App
  • Configure Database Detail
  • Run Migration
  • Install laravel-dompdf Package
  • Generate Fake Data in Table
  • Create Routes
  • Generate Controller By Artisan
  • Create Blade View
  • Start Development Server

Install Laravel Application

First Step, you need to download or install laravel web application new setup on your local machine. you can use the following command to download or install laravel application new setup:

composer create-project laravel/laravel laravel-pdf –-prefer-dist

If you want to install a specific version of laravel, you can follow the following tutorial

https://w3alert.com/laravel-tutorial/laravel-install-on-windows-ubuntu-with-composer

Configure Database Detail

After that, you will set up the database credentials in .env file. So open your .env file and update database credentials following:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=root
DB_PASSWORD=root

Run Migration

Next, go to command prompt and run the following command:

php artisan migrate

OR

php artisan migrate:fresh

This php artisan migrate command creates a table in your database.

Install laravel-dompdf Package

Next, you need to Install laravel barryvdh dompdf Package in your laravel web applications. So go to your command prompt and run the following command to install the latest laravel Install barryvdh dompdf Package:

composer require barryvdh/laravel-dompdf

When the package of laravel-dompdf Package is installed. After that this package will have to be registered in the Laravel web application of the app. For this, you can register this package of instruction follow below.

App_root_directory/config/app.php

 'providers' => [
 
 Barryvdh\DomPDF\ServiceProvider::class,

 ],
 
'aliases' => [
 
 'PDF' => Barryvdh\DomPDF\Facade::class,
 
 ]

When you will register the package of Maatwesite. After that, the package will have to be published with the command following command.

Generate Fake data in Table

Next, we need to generate fake data into the MySQL database table by using php artisan command. So go to your command prompt (cli) and then go to your project root directory. After that run the following command:

php artisan tinker

After run the php artisan tinker. Use the following command. This command will add 200 fake data in your MySQL database table users:

>>> factory(App\User::class, 200)->create();

Create Routes

Next step, go to routes/web.php file and update the following routes into your file:

use App\Http\Controllers\GeneratePDFController;


Route::get('generate-pdf', [GeneratePDFController::class, 'pdf']);

Generate Controller By Artisan

Now, you need to generate or create a controller file using php artisan command, so use the php artisan make:controller command and generate/create a new controller file:

php artisan make:controller GeneratePDFController

In this controller file, we will create new method. This method will create pdf.

After that, go to App/Http/Controllers/ directory and find the created GeneratePDFController file and update the following code into your file:

<?php
   
namespace App\Http\Controllers;
   
use App\Models\User;
use Illuminate\Http\Request;
use Redirect;
use PDF;
   
class GeneratePDFController extends Controller
{
   
    public function pdf(){
      
     $data['users'] =  User::get();
 
     $pdf = PDF::loadView('users-list', $data);
   
     return $pdf->download('sample.pdf');
    }
    
 
}

Create Blade Views

Next, you need to create a blade views file, so go resource/views directory and create the following blade views file.

Go to resource/views/ then create a new file name users-list.blade.php and update the following code:

<table class="table table-bordered">
 <thead>
    <tr>
       <th>Id</th>
       <th>Name</th>
       <th>Email</th>
       <th>Created at</th>
        
    </tr>
 </thead>
 <tbody>
    @foreach($users as $user)
    <tr>
       <td>{{ $user->id }}</td>
       <td>{{ $user->name }}</td>
       <td>{{ $user->email }}</td>
       <td>{{ date('d m Y', strtotime($user->created_at)) }}</td>
         
    </tr>
    @endforeach
 </tbody>
</table>

Start development server

You can use the following command artisan command to start the development server:

php artisan serve

The final step, go to your browser and type the below-given URL into your browser:

http://localhost:8000/generate-pdf

OR

http://localhost/laravel-pdf/public/generate-pdf