Site Admin
Site Admin Founder of MeaningArticles
1092 Views

Generate PDF from HTML with Dompdf in Laravel

Hello Dev.

PDF is a document format created by Adobe systems for illustrating text and images in a fixed-format file. PDF is used to downloading the bunch of data or textual content material in the web application. PDF document layout is the precise choice to download the textual content or HTML content in a file. on the time of download web page content material as a PDF file, it requires changing HTML to PDF. on this article, we are able to show you a way to convert HTML to PDF and generate PDF file using laravel.

Dompdf is a php library that gives a simple way to convert HTML to PDF document. the use of the Dompdf library you can without problems generate PDF from the HTML page in laravel. the instance code will help you to implement PDF generation functionality in the web application and make it simple to convert HTML to PDF in laravel with Dompdf.

So let's start the lesson...

 

Step 1: Install Laravel

First of all we want to get clean and new Laravel application the use of bellow command, now open your terminal OR command prompt and then fire bellow command and run it:

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

 

Step 2: Install barryvdh/laravel-dompdf Package

After installation of project you need to install barryvdh/laravel-dompdf Package

composer require barryvdh/laravel-dompdf

 

Step 3: Add Service Provider And Aliase

After package installation we need to add service provider and aliase in  config/app.php.

app.php

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

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

 

Step 4: Create Controller

Now create controller on this path app\Http\Controllers\UserController.php and add below command.

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 add below code in routes/web.php

web.php

Route::resource('users','UserController');

 

Step 6: Create Blade File

Now, create index.blade.php file for download and generate pdf file in this path resources\views\users\index.blade.php abd add below html code.

index.blade.php

<!DOCTYPE html>
<html>
<head>
  <title>Generate And Download PDF File Using dompdf - meaningarticles.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>Generate And Download PDF File Using dompdf - meaningarticles.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>

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

I hope it assists you, thanks for visiting my article if you like my article then share it with your friends on the social media platform.

Happy Coding.....