Laravel 8 Generate Pdf From Html View file And Download

Published By: Admin, Published On: May 31, 2021, Category: Laravel 8 Laravel

Hello Friends,

Now let's see example of how to generate pdf from html view file in laravel example. This is a short guide on laravel pdf from html view file. Here you will learn generate pdf from html view file laravel. We will use generate pdf from html view file in laravel. Let's get started with generate pdf from html view file in laravel.

Here i will give you few step instruction to generate pdf from html view file in laravel.

Step 1: Install Package

In this step, we will do Installation. We will firstly open our terminal or command prompt and run the following command:

composer require barryvdh/laravel-dompdf   --ignore-platform-reqs

Now we will open our file named config/app.php. Then we will add aliased and service provider.

Path: config/app.php

'providers' => [  
'aliases' => [  
    'PDF' => Barryvdh\DomPDF\Facade::class,  
Step 2: Add Route

In this step, we will Add Route, We will create route so that we can generate a view,

Path: app/Http/routers.php


    use Illuminate\Support\Facades\Route;
    use App\Http\Controllers\ItemController;

    | Web Routes
    | Here is where you can register web routes for your application. These
    | routes are loaded by the RouteServiceProvider within a group which
    | contains the "web" middleware group. Now create something great!
Step 3: Create Controller

In this step, we will Create Controller. For this, we are going to use the app/Http/Controllers/ItemController.php path to add a new controller as ItemController.

Path: app/Http/Controllers/ItemController.php


    namespace App\Http\Controllers;
    use App\Http\Requests;  
    use Illuminate\Http\Request;
    use DB;  
    use PDF;  

    class ItemController extends Controller
        * Write code on Method
        * @return response()
        public function itemPdfView(Request $request)  
            $items = DB::table("items")->get();  
                $pdf = PDF::loadView('itemPdfView');  
                return $pdf->download('itemPdfView.pdf');  
            return view('itemPdfView');  
Step 4: Create BladeFile

In this step, we will Create View file. We will create view and pdf file by using the view file named as "pdfview.blade.php".

Path: resources/view/itemPdfView.blade.php

<!DOCTYPE html>
    <title>Generate PDF from html view file and download using dompdf in Laravel</title>  
<style type="text/css"> 
        width: 100%;
        border-collapse: collapse;
    table td, table th{  
        border:1px solid black;
    table tr, table td{
        padding: 5px;
    <div class="container">  
    <a href="{{ route('itemPdfView',['download'=>'pdf']) }}">Download PDF</a>     
        @foreach ($items as $key => $item)  
                <td>{{ ++$key }}</td>  
                <td>{{ $item->name }}</td>  
                <td>{{ $item->price }}</td>  

Using the above code we are able to convert item file into pdf file and also able to download that file.For this, we will open our terminal or command prompt and run the following command:

php artisan serve  

It will help you....