Laravel 8 PDF Generate Example

Websolutionstuff | Oct-17-2020 | Categories : Laravel jQuery

In this article, we will see a laravel 8 pdf generate example. For generating PDF file we will use the laravel-dompdf package, which creates pdf file and also provide download file functionalities. it is very easy to generate pdf in laravel 8. So, we will give you an example of a very simple way to generate a pdf file and download them into your system.

So, let's see how to generate a pdf in laravel 8 and a pdf generate in laravel 8.

Laravel 8 Generate PDF File Using DomPDF

Step 1: Install Laravel 8 For Generate PDF File

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

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

 

Step 2: Install barryvdh/laravel-dompdf Package In Your Application

In this step, we will install the barryvdh/laravel-dompdf package using the composer command.

composer require barryvdh/laravel-dompdf

 

 

Step 3: Add Service Provider And Aliase

After package installation, we will add the service provider and aliase in the config/app.php file.

'providers' => [
	
	Barryvdh\DomPDF\ServiceProvider::class,
],

'aliases' => [
	
	'PDF' => Barryvdh\DomPDF\Facade::class,
],

 

Step 4: Create Controller

Now, we will create a new controller.

app/Http/Controllers/UserController.php

<?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::latest()->paginate(5);
  
        if($request->has('download'))
        {
            $pdf = PDF::loadView('users.index',compact('user'));
            return $pdf->download('pdfview.pdf');
        }

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

 

 

Step 5: Add Route

After that, we will add the below code in the web.php file.

routes/web.php

Route::resource('users','App\Http\Controllers\UserController');

 

Step 6: Create Blade File

In this step, we will create an index.blade.php file for download and generate a pdf file.

resources/views/users/index.blade.php

<!DOCTYPE html>
<html>
<head>
  <title>Laravel 8 PDF Generate Example - websolutionstuff.com</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>Laravel 8 PDF Generate Example - websolutionstuff.com</h2>
        </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>

 

 

 Output:

laravel_8_pdf_generate_example

 


You might also like:

Recommended Post
Featured Post
How To File Upload In Angular 15 Example
How To File Upload In Angular...

As an Angular 15 developer, I understand the significance of incorporating file upload functionality into web applicatio...

Read More

Jun-19-2023

Call To Undefined Function mb_strcut() In Laravel
Call To Undefined Function mb_...

Laravel is a fantastic language for web development, but it can throw you a curveball every now and then. If you've...

Read More

Jan-09-2023

How To Get Last 30 Days Record In Laravel 8
How To Get Last 30 Days Record...

in this tutorial, we see how to get last 30 days record in laravel 8. You can simply get the last 30 days reco...

Read More

Feb-02-2022

How to Manage Time Zones in React.js Applications
How to Manage Time Zones in Re...

In the interconnected world of web development, where applications are accessed by users spanning multiple time zones, t...

Read More

Sep-04-2023