Laravel 10 PDF Generator - Generate PDF in Laravel 10

Laravel 10 PDF Generator – Generate PDF in Laravel 10

In this article, I am going to walk you through the steps of generating PDF in Laravel 10, and we sill review Laravel 10 PDF Generator. 

I will explain every step that is needed for an absolute beginner to convert the HTML to PDF.

We will use DomPDF converter to generate pdf, this converter, is a CSS 2.1 compliant HTML layout &rendering engine. And will show you how to install and set it up.

Let’s get into the steps.

Step 1: download laravel

First thing first, install laravel 10 by the following command

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

Navigate to your project and set up your database.

Step 2: set up database configuration

In this project, we need some dummy data to generate from them.Create your database first and go to your project root directory and open .env file and change the following attributes

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

Step 3: Generate some dummy data for the user table

In this step, before creating our controller, we are going to generate some dummy data using thinker in laravel 10

php artisan tinker
User::factory()->count(12)->create()

We have successfully created 12 records for our user’s table with the power of factories in laravel 10.

Step 4: Create UsersController

In this step, we will generate a controller by the name of UsersController to Generate PDF in Laravel 10 from our users, in this controller we will list our users and return them to the index page. On the index page, we will have our users list and a download button to download all users list in a PDF File.

So, let’s create our controller using the below command.

php artisan make:controller UsersController

The above command will generate a UsersController named file in “App/Http/Controllers” directory. Open the file and paste the following files into it.

<?php

namespace App\Http\Controllers;

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

class UsersController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function usersList()
    {
        $users = User::latest()->paginate(12);

        return view('users_list', compact('users'))
            ->with('i', (request()->input('page', 1) - 1) * 12);
    }

    /**
     * Generate PDF file from the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function usersPDF()
    {
        $users = User::get();

        $data = [

            'date' => date('m/d/Y'),
            'users' => $users
        ];

        $pdf = PDF::loadView('pdf', $data);

        return $pdf->download('CodeWithJA-Users-List.pdf');
    }
}

Step 4: Views

We will create two views for this project, one view file for listing our users, and one view for our pdf. In bigger projects the listing view might be different from the pdf view, therefore I create two view files for each, you can change the list view or pdf view as you want.

users_list.blade.php

<!DOCTYPE html>
<html>

<head>
 <title>Laravel 10 PDF Generator - Generate PDF in Laravel 10</title>
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>

<body>
 <h1>Laravel 10 Generate PDF usin DOMPdf</h1>

 <p>CodeWithJA.com users list, to generate a PDF File from this list, please click <a href="{{ route('usersPDF') }}">Here</a>.</p>

 <table class="table table-bordered">
  <tr>
   <th>ID</th>
   <th>Name</th>
   <th>Email</th>
  </tr>
  @foreach($users as $user)
  <tr>
   <td>{{ $user->id }}</td>
   <td>{{ $user->name }}</td>
   <td>{{ $user->email }}</td>
  </tr>
  @endforeach
 </table>

</body>

</html>

pdf.blade.php

<!DOCTYPE html>
<html>

<head>
 <title>Laravel 10 PDF Generator - Generate PDF in Laravel 10</title>
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>

<body>
 <h1>Laravel 10 Generate PDF using DOMPdf </h1>
 <p>CodeWithJA.com users list!</p>
 <div class="container">

  <table class="table table-bordered">
   <tr>
    <th>ID</th>
    <th>Name</th>
    <th>Email</th>
   </tr>
   @foreach($users as $user)
   <tr>
    <td>{{ $user->id }}</td>
    <td>{{ $user->name }}</td>
    <td>{{ $user->email }}</td>
   </tr>
   @endforeach
  </table>

 </div>
</body>

</html>

Step 5: Routes

In this step, we will create two routes, one for user listing and one for users pdf list.

To edit the route file, go to the “routes” folder in the root directory of your project and open web.php, then paste the following route codes into it

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\UsersController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('users-PDF', [UsersController::class, 'usersPDF'])->name('usersPDF');
Route::get('users-list', [UsersController::class, 'usersList']);

Step 6: Final Step

we are done with Laravel 10 PDF Generator – Generate PDF in Laravel 10

open your command prompt, start your project by below command and see the Laravel 10 PDF Generator result.

php artisan serve

use the following link to see your project result

localhost:8000/users-list

Thanks for reading, if you have any question about how to Gnerate PDF in Laravel 10 ask in the comment box.

Also read: Image / File upload in Laravel 10 with codewithja.com

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *