How To Generate PDF Using DomPDF In Laravel 9

In this article, we will see how to generate pdf using dompdf in laravel 9/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 9 and laravel 10. we will see an example of a very simple way to generate a pdf file and download it to your system.

Using 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 9/10.

So, let's see laravel 9 generates a pdf file using dompdf.

How To Generate PDF In Laravel 9/10 Using DomPDF

Step 1: Install Laravel To Create PDF In Laravel 9

Step 2: Install barryvdh/laravel-dompdf Package

Step 3: Create Controller

Step 4: Add Route

Step 5: Create Blade File

Step 6: Run the Laravel 9 DomPDF Application

 

Step 1: Install Laravel To Create PDF In Laravel 9

In this step, we will install 9 using the following command.

composer create-project --prefer-dist laravel/laravel laravel_9_generate_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 the following code to generate a pdf file.

app\Http\Controllers\UserController.php

<?php

namespace App\Http\Controllers;

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

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

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

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

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

 

 

Step 4: Add Route

Now, we will add the below code in the web.php 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 9 Generate PDF File 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 ($user as $user)
      <tr>
        <td>{{ $user->name }}</td>
        <td>{{ $user->email }}</td>
      </tr>
      @endforeach
    </table>
  </div>
</body>
</html>

 

Step 6: Run the Laravel 9 DomPDF Application

Now, run this laravel 9 Create PDF using the DomPDF application using the artisan command.

php artisan serve

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS