Laravel 10 Generate PDF Using DomPDF

In this article, we will see laravel 10 generate pdf using dompdf. Here, we will learn about how to generate pdf using dompdf in laravel 10. For generating pdf files we will use the laravel-dompdf package.

It creates a pdf file and also provides download file functionalities. It is very easy to generate pdf files in laravel 10. we will see an example of a very simple way to generate a pdf file and download it to your system.

barryvdh/laravel-dompdf pdf you can create a new DomPDF instance and load an HTML string, file or view name. You can save it to a file, or show it in the browser or download it. Also, you can generate pdf from HTML view in laravel 10.

So, let's see generate pdf in laravel 10, laravel 10 pdf generate, how to create a pdf in laravel 10.

How To Generate PDF In Laravel 10 Using DomPDF

Step 1: Install Laravel 10

Step 2: Install barryvdh/laravel-dompdf Package

Step 3: Create Controller

Step 4: Add Route

Step 5: Create Blade File

Step 6: Run Laravel 10 Application

 

Step 1: Install Laravel 10

In this step, we will install 10 using the following command. So, run the below code to the terminal.

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

 

 

Step 2: Install barryvdh/laravel-dompdf Package

Now, we will install the barryvdh/laravel-dompdf Package using the composer command.

composer require barryvdh/laravel-dompdf

 

Step 3: Create Controller

In this step, we will create a new controller and add the following code to generate a pdf file.

app/Http/Controllers/UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use PDF;

class UserController extends Controller
{    
    public function index(Request $request)
	{
	    $users = User::get();

	    $data = [
	            'title' => 'How To Create PDF File In Laravel 10 - Techsolutionstuff',
	            'date' => date('d/m/Y'),
	            'users' => $users
	    ];

	    if($request->has('download'))
	    {
	        $pdf = PDF::loadView('index',$data);
	        return $pdf->download('users_list.pdf');
	    }

	    return view('index',compact('users'));
	}
}

 

 

Step 4: Add Route

Now, we will add the routes in the web.php file. So, add the below code to that file.

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\UserController;
  
Route::resource('users', UserController::class);

 

Step 5: Create Blade File

In this step, we will create an index.blade.php file for download and generate a pdf file.

resources/views/index.blade.php

<!DOCTYPE html>
<html>
<head>
  <title>Laravel 10 Generate PDF Using DomPDF - Techsolutionstuff</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
  <div class="container">
    <div class="row">
      <div class="col-lg-12" style="margin-top: 15px ">
        <div class="pull-left">
          <h2>{{$title}}</h2>
          <h4>{{$date}}</h4>
        </div>
        <div class="pull-right">
          <a class="btn btn-primary" href="{{route('users.index',['download'=>'pdf'])}}">Download PDF</a>
        </div>
      </div>
    </div><br>
    <table class="table table-bordered">
      <tr>
        <th>Name</th>
        <th>Email</th>
      </tr>
      @foreach ($users as $user)
      <tr>
        <td>{{ $user->name }}</td>
        <td>{{ $user->email }}</td>
      </tr>
      @endforeach
    </table>
  </div>
</body>
</html>

 

Step 6: Run the Laravel 10

Now, we will run laravel 10 create PDF using DomPDF application using the artisan command.

php artisan serve

 


You might also like:

RECOMMENDED POSTS

FEATURE POSTS