More

How to download/generate pdf file using Laravel 8 Dompdf


Normally, when we work on large project on laravel then we require to generate PDF file of database table. So, In this tutorial we will learn about Laravel pdf package ( laravel dompdf ) using which we can download pdf file .

Here, we use laravel dompdf for create pdf and this package also provide the function of download.

image
 

Now follow all the below steps to download pdf using laravel 8 .

image

Step 1 – In first step , Install dompdf package 

Run this command in your cmd terminal –

composer require barryvdh/laravel-dompdf

how to download pdf using laravel 8 dompdf


 

Step 2 - In second step open config/app.php file and update package service provider and alias.

 

    'providers' => [

        ....

        Barryvdh\DomPDF\ServiceProvider::class,

    ],

      

    'aliases' => [

        ....

        'PDF' => Barryvdh\DomPDF\Facade::class,

    ] 

 



Step 3 – Create Controller

php artisan make:controller PdfController


app\Http\Controllers\PdfController.php

<?php

 

namespace App\Http\Controllers;

Use PDF;

use Illuminate\Http\Request;

 

class PdfController extends Controller

{

    public function downloadPDF(){

        $data = [

           "full_name"=> "john do",

           "email"=> "john@example.com",

           "age"=>"23",

           "phone"=>"998877XXXX",

        ];

        $pdf = PDF::loadView('Sample_pdf',$data);

        return $pdf->download('teknowize.pdf');

    }

}  



Step 4- Create Route

use App\Http\Controllers\PdfController;

use Illuminate\Support\Facades\Route;

 

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

*/

 

Route::get('download-pdf', [PdfController::class'downloadPDF']); 

 



Step 5 –  Create pdf view template 

In this step , Create Sample_pdf.blade.php in resources\views .

resources\views\Sample_pdf.blade.php

<!DOCTYPE html>

<html>

<head>

    <title>laravel-pdf</title>

    <style>

    table tr th{

      background-color:#04AA6D;

      color:white;

      border1px solid #ddd;

      padding8px;

    }

    table tr td{

      border1px solid #ddd;

      padding8px;

    }

    </style>

</head>

<body>

    <h3>Laravel 8 PDF Example Using dompdf </h3>

   <table>

       <tr>

           <th>Full Name</th>

           <th>Email Id</th>

           <th>Age</th>

           <th>Phone Number</th>

       </tr>

       <tr>

           <td>{{ $full_name }}</td>

           <td>{{ $email }}</td>

           <td>{{ $age }}</td>

           <td>{{ $phone }}</td>

       </tr>

   </table>

</body>

</html>  



Step 6 – Run php artisan serve

php artisan serve

image

Finally , run this url in your browser

http://127.0.0.1:8000/download-pdf

 

Related Topics

×

TOP TOPICS

Road map for web development 2022 What is git and git hub How to create a Google logo using HTML and CSS What is .htaccess file and how to use it Most important HTML5 interview questions PHP CRUD operations How to send mail using PHP Insert and fetch image using PHP How to dowload pdf using PHP How to export data in Excel using Laravel Import excel data in database using laravel How to send mail in Laravel 8 Laravel 8 CRUD Operation Laravel 8 Image Upload and Fetch Tutorial Example Laravel 8 CRUD operation using DB Laravel 8 CRUD Operation using resource controller Date Validations Using jquery Datepicker How to reset or clear a form using JavaScript Form validation in Laravel 8 How to download pdf file using Laravel 8 dompdf How to Toggle Password Visibility Using JavaScript PHP CRUD Operation Using Ajax and Jquery Get Yesterday and Tomorrow Date in PHP How to disable text selection using CSS and Jquery How to Get Previous Month and Next month date from Date in PHP Laravel CRUD Operation Using Ajax and Jquery How to find numbers from string in PHP How to Convert String Date to Date Format in PHP How to calculate age from dob column using mysql Rotate image using JQuery animate() method Allow Only Numbers in a Text Box using jQuery How To connect SSH using PHP? Radio buttons value using GET method in PHP How to get X and Y position on click event using jQuery Delete Confirmation Message using JavaScript Select2 DropDown With Validation using Bootstrap 4 How to Zoom an Image on Mouse Hover using CSS How to Remove First Element from Array in PHP How to Remove Last Element from Array in PHP E-Signature Pad using jQuery, Ajax and PHP PHP Contact Form with Google reCAPTCHA How to update profile image in Laravel using Ajax How to get X and Y position of an element using jQuery How to disable a anchor tag in HTML using CSS, JS and jQuery check if atleast one checkbox is checked or not using jQuery How can we count number of rows in a table using jQuery How can we count child elements inside a div using jQuery How to use radio button on change event in jQuery How to get checked radio button value by name using jQuery How to count the number of character in a string using jQuery How to add modal popup in PHP using jQuery?