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 and laravel 11. For generating PDF files we will use the laravel-dompdf package.

In laravel 11, you can create PDF using the DomPDF package. Also, you can use it in laravel 10.

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/11.

So, let's see how to generate a PDF in laravel 10, and how to create a PDF in laravel 11.

How To Generate PDF In Laravel 10/11 Using DomPDF

Step 1: Install Laravel 10/11

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/11

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/11 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 to create a 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