DEV Community


How Generate PDF From HTML View In Laravel

I am Laravel Developer. I have good knowledge of JavaScript, JQuery, Bootstrap and REST API.
Originally published at ・2 min read

In this example I will teach you how to generate PDF file from HTML view in laravel.

For generating PDF file I will use Laravel-dompdf package, it is create PDF file and also provide to download file functionalities, i will give you example in very simple way to generate PDF file and download in your system. So, Let's start and follow below steps to get output.

Step 1 : Install Laravel

Type the following command in terminal for create new project in your system.

composer create-project --prefer-dist laravel/laravel PDF_example
Enter fullscreen mode Exit fullscreen mode

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

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

Read Also : How To Generate QRcode In Laravel

composer require barryvdh/laravel-dompdf
Enter fullscreen mode Exit fullscreen mode

Step 3 : Add Service Provider And Aliase

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

'providers' => [


'aliases' => [

    'PDF' => Barryvdh\DomPDF\Facade::class,
Enter fullscreen mode Exit fullscreen mode

Step 4 : Create Controller

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


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);

            $pdf = PDF::loadView('users.index',compact('user'));
            return $pdf->download('pdfview.pdf');

        return view('users.index',compact('user'));
Enter fullscreen mode Exit fullscreen mode

Step 5 : Add Route

After that add below code in routes/web.php

Enter fullscreen mode Exit fullscreen mode

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 add below html code.

<!DOCTYPE html>
  <title>Generate And Download PDF File Using dompdf -</title>
  <link rel="stylesheet" href="">
  <script src=""></script>

  <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 -</h2>
        <div class="pull-right">
          <a class="btn btn-primary" href="{{route('users.index',['download'=>'pdf'])}}">Download PDF</a>

    <table class="table table-bordered">

      @foreach ($user as $user)
        <td>{{ $user->name }}</td>
        <td>{{ $user->email }}</td>
Enter fullscreen mode Exit fullscreen mode

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

Read Also : Import Export CSV/EXCEL File In Laravel

Discussion (0)