Generate PDF File Using DomPDF laravel 9

In this tutorial, we will see laravel 9 generate pdf file using dompdf. for generating pdf file and viewer.

In this series of articles, we have covered about:
Web Application Model Data Model Data Processing Data Storage. Generate PDF File Using DomPDF laravel 9. Documentation for Laravel and django. Please note that these articles are aimed towards absolute beginners and are not really appropriate for advanced developers. If you have any doubt, feel free to drop in a comment and I’ll be happy to discuss it with you!
And that’s all for today!

 Step 1 : Install Laravel 9 of Generate PDF File Using DomPDF laravel 9

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

Step 2 : Installing barryvdh/laravel-dompdf Package

composer require barryvdh/laravel-dompdf

Step 3 : Create Controller

i am happy for contoller created done so i can pdfcontoller.php

<?php

namespace App\Http\Controllers;

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

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

	    $data = [
	            'maintitle' => 'Laravel 9 Generate PDF File Using DomPDF - laratuto',
	            'maindate' => date('d/m/Y'),
	            'activeusers' => $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 add route in api.php

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

Step 6 : Create Blade File

<!DOCTYPE html>
<html>
<head>
  <title>Laravel 9 Generate PDF File Using DomPDF - laratuto</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>{{maintitle}}</h2>
          <h4>{{maindate}}</h4>
        </div>
        <div class="pull-right">
          <a class="btn btn-primary" href="{{route('users.index',['download'=>'pdf'])}}">Download PDF Now</a>
        </div>
      </div>
    </div><br>
    <table class="table table-bordered">
      <tr>
        <th>User Name</th>
        <th>User Email</th>
      </tr>
      @foreach ($user as $user)
      <tr>
        <td>{{ $user->mainname}}</td>
        <td>{{ $user->mainemail}}</td>
      </tr>
      @endforeach
    </table>
  </div>
</body>
</html>

I happy if this project run or pdf download done..

Please find your questions and my answers to your questions at the next link below:
Got an idea for a new CMS/AJAX project? Maybe you would like to develop a blog engine or mobile application? Do you have some tips and tricks you would like to share with the PHP community? Or maybe you would like to talk to other aspiring PHP developers about their projects? If so, feel free to contact me!
Pragmatic Collaboration PHP Network is a new