The focus of this example is on the laravel 8 pdf file from viewLaravel 8 generate pdf file is a concept that you may grasp. I described laravel 8 pdf dompdf in simple terms. This post will show you how to generate a PDF document in Laravel 8.
Here, Using Laravel 8 to create a PDF from a View.
When working on an ERP project or an e-commerce website, one of the most basic requirements is PDF. It's possible that we'll need to create a pdf file for a report or an invoice, for example. So, in this example, I'll show you how to make a PDF file with Laravel in a very simple way.
To make a pdf file that you can also download, simply follow the steps below. Let's get started with the instructions below.

Table of Content

  1. Install Laravel 8
  2. Install dompdf Package
  3. Add Route
  4. Add Controller
  5. Create View File

Step 1: Install Laravel 8

I'll walk you through everything step by step. First, we'll create a new Laravel 8 application with the commands below. So, open a terminal OR a command prompt and type the following command:

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

 

Step 2: Install dompdf Package

First, we'll use composer to install the barryvdh/laravel-dompdf composer package into your Laravel 8 website.

composer require barryvdh/laravel-dompdf

After the package has been properly installed, edit the config/app.php file and add the service provider and alias.

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

 

Step 3: Add Route

We must design routes for item listing in this step. So, go to "routes/web.php" and add the following route.

routes/web.php
<?php
  use Illuminate\Support\Facades\Route;
  use App\Http\Controllers\PDFController;
  /*
 |--------------------------------------------------------------------------
 | 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!
 |
 */
  
 Route::get('generate-pdf', [PDFController::class, 'generatePDF']);

 

Step 4: Add Controller

We'll need to develop a new controller, PDFController, to manage the route's generatePDF method. So, let's get started with the code below.

app/Http/Controllers/PDFController.php
<?php
 namespace App\Http\Controllers;
 use Illuminate\Http\Request;
 use PDF;
  
class PDFController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function generatePDF()
    {
        $data = [
            'title' => 'Welcome to CodeSolutionStuff.com',
            'date' => date('m/d/Y')
        ];
          
        $pdf = PDF::loadView('myPDF', $data);
    
        return $pdf->download('codesolutionstuff.pdf');
    }
}

 

Step 5: Create View File

Finally, build myPDF.blade.php (resources/views/myPDF.blade.php) for the pdf file layout and add the following code:

resources/views/myPDF.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Hi</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>

We're now ready to execute and test this example.

I hope you will like the content and it will help you to learn Laravel 8 Generate PDF File using DomPDF | Laravel 8 PDF
If you like this content, do share.


Recommended Posts

View All

Laravel whereBetween Query Example


In this post, we will see Laravel whereBetween query model. SQL gives numerous different sorts of techniques or queries to get separated information f...

Laravel 9 Livewire Form Submit Tutorial


Laravel 9 Work With Livewire Form Submit Tutorial. laravel 9 livewire form submit example, create form with laravel 9 livewire, laravel 9 livewire tut...

Laravel 9 Socialite Login with Facebook Tutorial


In this tutorial, we'll learn how to use the Socialite package in Laravel to login with a Facebook social networking account.

How To Use WYSIHTML5 Editor In Laravel 9?


wysihtml5 editor example, laravel wysihtml5 editor example, wysihtml5 laravel, wysihtml5 editor example in laravel, wysihtml5 with laravel, wysihtml5...

Laravel 9 Image Resize & Upload with Intervention Image


This tutorial will show you how to use the PHP intervention image package to resize an image in a Laravel application.