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

Unlocking the Power of Data Visualizing Laravel App Data with Chartello


Discover the potential of data visualization with Chartello, a Laravel app that helps you unlock insights from your data. Try it now and see the resul...

Laravel Google Chart Example Tutorial


laravel google chart example, dynamic charts in laravel, laravel google pie chart, laravel google line chart example, google chart in laravel, laravel...

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 8 Stripe Subscription Tutorial Using Cashier Example


laravel cashier, stripe laravel, laravel subscription tutorial, laravel 8 cashier tutorial, laravel 8 stripe subscription tutorial

Laravel 9 Create Multi Language Website Example Tutorial


Learn how to create a multi-language website with Laravel 9 through our example tutorial. Develop your skills and broaden your audience reach today!