How to Create PDF File in Laravel 9?

Feb 15, 2022 . Admin

Hi friends,

In this tutorial how to create pdf file in laravel 9?. We will learn how to generate PDF from HTML using the DomPDF library. We will use laravel 9 create pdf file using dompdf. In this tutorial, we will discuss about the Laravel 9 Export to PDF topic.

This step by step tutorial allows you to explore the limitless opportunity if you are a newbie laravel developer. In general, PDF files are used to provide some information to the users.

You can generate a pdf file from a view using DomPDF. To export into PDF, We need to write the view file. Then, we will write the HTML code and load data dynamically from the database as per the requirement. After that, we will export this view as a PDF file.

Let’s get started.

Step 1: Download Laravel

Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app
Step 2: Install DomPDF Package

next, we will install DomPDF package using following composer command, let's run bellow command:

composer require barryvdh/laravel-dompdf
Step 3: Create Controller

In this step, we will create PDFController with generatePDF() where we write code of generate pdf. so let's create controller using bellow command.

php artisan make:controller PDFController

in PDFController, we also get users table data and display them into pdf file. so you can add some dummy data on the users table by using the following tinker command:

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

Now, update the code on the controller file.

app/Http/Controllers/PDFController.php
<?php

namespace App\Http\Controllers;

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

class PDFController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
    */
    public function generatePDF()
    {
        $users = User::get();
  
        $data = [
            'title' => 'Welcome to Mywebtuts.com',
            'date' => date('m/d/Y'),
            'users' => $users
        ]; 
            
        $pdf = PDF::loadView('myPDF', $data);
     
        return $pdf->download('mywebtuts.pdf');
    }
}
Step 4: Create Route

Furthermore, open routes/web.php file and update code on it.

routes/web.php
<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\PDFController;
  
/*
|--------------------------------------------------------------------------
| 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('generate-pdf', [PDFController::class, 'generatePDF']);
Step 5: Create View File

In Last step, let's create myPDF.blade.php(resources/views/myPDF.blade.php) for layout of pdf file and put following code:

resources/views/myPDF.blade.php
<!DOCTYPE html>
<html>
<head>
    <title>Laravel 9 Create PDF File using DomPDF Tutorial - Mywebtuts.com</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>{{ $title }}</h1>

    <p>{{ $date }}</p>

    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua.</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>
Run Laravel App:

All steps have been done, now you have to type the given command and hit enter to run the laravel app:

php artisan serve

Now, you have to open web browser, type the given URL and view the app output:

http://localhost:8000/generate-pdf

you will downloaded file as like bellow:

Preview

Now we are ready to run this example and check it...

I hope it can help you...

#Laravel 9