Laravel 8.0 PDF | Laravel 8.0 Generate PDF File example

This example focuses on the view of the Laravel 8.0 pdf file. We will learn how to Generate PDF File example. You can understand the idea of generating pdf file with laravel 8.0 . I was just explaining about Laravel 8 pdf dompdf. In laravel 8.0, this article will give you a clear example of how to generate pdf.

Here, create a pdf from view by creating a simple example of Laravel 8.

Step 1: Install Laravel 8

I am going to explain step by step from scratch so, we need to get fresh Laravel 8.0 application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel blog
You can read also How to create laravel 8.0 project example

Step 2: Install dompdf Package

You can find the dompdf here. Generate pdf in laravel 8.0 is very simple and easy using dompdf. It is very efficient way to generate pdf in laravel 8.0.

composer require barryvdh/laravel-dompdf

After successfully install package, open config/app.php file and add service provider and alias.

config/app.php
'providers' => [

	....
        ....
	Barryvdh\DomPDF\ServiceProvider::class,

],

  

'aliases' => [

	....

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

]

Step 3: Add Route

routes/web.php
Route::get('/pdf',[PdfController::class,'generatePDF'])->name('generatePDF');

Step 4: Create Controller

By running bellow command, we will create our PdfController

php artisan make:controller PdfController

app\Http\Controllers\PdfController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use PDF;
class PdfController extends Controller
{
    public function generatePDF(){
        $data = [
            'title' => 'Welcome to codesnipeet.com',
            'date' => date('m/d/Y')
        ];
        $pdf = PDF::loadView('mypdf', $data);
        return $pdf->download('example-pdf.pdf');
    }
}

Step 5: Create View File

resources/views/mypdf.blade.php

<!DOCTYPE html>

<html>

<head>

    <title>Generate-PDF</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>

visite:

http://localhost:8000/pdf

A dialog box appear with wants to download pdf file. Check it..if not works pleace check again steps by steps

Hope it will help you!

3 replies to Laravel 8.0 PDF | Laravel 8.0 Generate PDF File example

  1. Pingback:

  2. Pingback:

  3. Pingback:

Leave a Reply