Laravel 11 Generate PDF File using DomPDF

Laravel 11 PDF generate from view or html; Through this tutorial, i am going to show you how to generate pdf from view or html in Laravel 11 apps using domPDF library.

This example post, you will learn how to generate pdf file from views or html in on Laravel 11 using dompdf package. And how to download pdf file in Laravel 11.

Laravel 11 Generate PDF File From View or HMTL

Use the below given simple steps to generate or create pdf file from views or hmtl in Laravel 11 apps:

  • Step 1 – Install Laravel 11 Application
  • Step 2 – Database Configuration
  • Step 3 – Install DomPDF Package
  • Step 4 – Configure DOMPDF Package
  • Step 5 – Create PDF Routes
  • Step 6 – Creating PDF Controller
  • Step 7 – Create Blade View File
  • Step 8 – Run Development Server

Step 1 – Install Laravel 11 Application

In step 1, install Laravel 11 new setup. So, open terminal and type the following command to install new Laravel 11 app into your machine:

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

Step 2 – Database Configuration

In step 2, configure database with your downloded/installed laravel app. So, you need to find .env file and setup database details as following:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db name
DB_USERNAME=db user name
DB_PASSWORD=db password

Step 3 – Install domPDF Package

In step 3, install DOMPDF package in laravel app via following command:

composer require barryvdh/laravel-dompdf

Step 4 – Configure DOMPDF Package

In step 4, configure the dompdf package in laravel app. So, Open the providers/config/app.php file and register the DOMPDF provider and aliases.

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

Step 5 – Create PDF Routes

In step 5, add the pdf generation routes in web.php file, which is located inside routes directory:

use App\Http\Controllers\PDFController;

Route::get('generate-pdf-from-view', [PDFViewController::class, 'index'])

Step 6 – Creating PDF Controller

In this step, create generate pdf controller file by using the following command:

php artisan make:controller PDFViewController

Then navigate to app/http/controllers and open PDFViewController.php file. And add the pdf generation code into it:

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;

use PDF;
  
class PDFViewController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = [
            'title' => 'Welcome to laratutorials.com',
            'date' => date('m/d/Y')
        ];
          
        $pdf = PDF::loadView('myPDF', $data);
    
        return $pdf->download('laratutorials.pdf');
    }
}

Step 7 – Create Blade File

In step 7, create blade view file for generate pdf from view. So, navigate to resources/views directory and create myPDF.blade.php and then add the html code into it:

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 11 Generate PDF From View</title>
</head>
<body>
    <h1>{{ $title }}</h1>
    <p>{{ $date }}</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
    quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
    consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
    cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
    proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</body>
</html>

Step 8 – Run Development Server

In step 8, open command prompt and type the below given command to run development server:

php artisan serve

Then open your browser and hit the following url on it:

http://127.0.0.1:8000/generate-pdf-from-view

Recommended Laravel Tutorials

Leave a Comment