Laravel 8 PDF – How to Generate PDF in Laravel 8

Laravel 8 PDF. In this post, i will show you how to generate pdf from view or html in laravel 8.

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

In this post, i will create views file, generate pdf controller file and routes to generate pdf file from views.

So, use the below given step to create pdf file from views or html and also can download.

Laravel 8 PDF | Laravel 8 Generate PDF File From View

  • Step 1 – Install Laravel 8 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 8 Application

In step 1, install laravel 8 new setup. So, open terminal and type the following command to install new laravel 8 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 8 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 8 app via following command:

composer require barryvdh/laravel-dompdf

Step 4 – Configure DOMPDF Package

In step 4, configure the dompdf package in laravel 8 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 8 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

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *