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

How to Install and Use MomentJS in Laravel 9 Application


Learn how to easily install and use MomentJS in your Laravel 9 application with our step-by-step guide. Improve your date and time management today!

Laravel 9 Captcha Tutorial – Create Captcha in Laravel


A CAPTCHA is a challenge-response test used in computing to determine if a user is human. It is an abbreviation for the Completely Automated Public Tu...

Laravel 9 Install React Auth Tutorial with Example


In this article, I'll demonstrate how to use the Laravel UI and React Auth scaffolding to create login, register, logout, forget password, profile, an...

Laravel 8 Create Custom Helper Functions


Laravel 8 Create Custom Helper Functions In this tutorial we will see how to Create Custom Helper Functions in Laravel 8.

Laravel 8 How To Merge Two PDF Files Example


laravel 8 how To merge two PDF files example,how to merge two PDF files in laravel 8,merge two PDF files,pdf,pdf in laravel 8