Laravel 9 Generate PDF File Using DomPDF

Websolutionstuff | Feb-25-2022 | Categories : Laravel PHP

In this tutorial, we will see laravel 9 generate pdf file using dompdf. 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 in laravel 9. I will give you an example in a very simple way to generate 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.

So, let's see laravel 9 generate pdf file using dompdf, download pdf in laravel 9, how generate pdf from html view in laravel 9.

 Step 1 : Install Laravel 9

Type the following command in the terminal to create a new laravel 9 project in your system.

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

 

 

Step 2 : Install barryvdh/laravel-dompdf Package

In this step, we need to install barryvdh/laravel-dompdf Package using the composer command.

composer require barryvdh/laravel-dompdf

 

Step 3 : Create Controller

Now create a new controller on this path app\Http\Controllers\UserController.php and copy the below command.

<?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' => 'Laravel 9 Generate PDF File Using DomPDF - Websolutionstuff',
	            'date' => date('d/m/Y'),
	            'users' => $users
	    ];

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

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

 

 

Step 5 : Add Route

After that add the below code in routes/web.php 

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

 

Step 6 : Create Blade File

In this last step, we will create an index.blade.php file for download and generate a pdf file in this path resources\views\index.blade.php and add below HTML code.

<!DOCTYPE html>
<html>
<head>
  <title>Laravel 9 Generate PDF File Using DomPDF - Websolutionstuff</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>

 After adding all code in your application you can download a pdf file of your HTML view.

 


You might also like :

Recommended Post
Featured Post
How To Increase Session Lifetime In Laravel
How To Increase Session Lifeti...

In this article, we will see how to increase session timeout in laravel. In this example, we can see how to se...

Read More

Jun-28-2020

Load More Data in Laravel Using Ajax jQuery
Load More Data in Laravel Usin...

In this article, we will see example of load more data in laravel using ajax jquery, In many websites, has huge content&...

Read More

Feb-26-2021

How to Integrate Razorpay Payment Gateway in Laravel
How to Integrate Razorpay Paym...

In this article, we will see the most important and exciting toping about how to integrate razorpay payment gateway in l...

Read More

Jan-06-2021

Laravel 9 whereColumn Query Example
Laravel 9 whereColumn Query Ex...

In this article, we will see laravel 9 whereColumn() query example. The whereCoulmn method is used to verify whethe...

Read More

Oct-21-2022