Laravel 10 Generate PDF File using barryvdh dompdf Example

If you are working in laravel application and you have to create pdf from html or pdf from blade view. What will you do then.

In this tutorial, you will learn how to generate or create pdf from view, blade, and, html in Laravel 10 apps using Barryvdh DomPDF.

How to Generate Or Create PDF File in Laravel 10 Using barryvdh dompdf

By following these steps, you can generate pdf from blade view and html in Laravel 10 using Barryvdh DomPDF package:

  • Step 1 – Create New Laravel 10 Project
  • Step 2 – Setup barryvdh/laravel-dompdf Package
  • Step 3 – Configure DOMPDF Package
  • Step 4 – Define PDF Routes
  • Step 5 – Create PDF Controller By Artisan Command
  • Step 6 – Create Blade View File
  • Step 7 – Run Development Server

Step 1 – Create New Laravel 10 Project

First of all, Open your terminal OR command prompt.

Then execute the following command into it to install new Laravel 10 app into your system:

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

Step 2 – Setup barryvdh/laravel-dompdf Package

In this step, open again your command prompt or terminal.

Then execute the following command into it. To install DOMPDF package:

composer require barryvdh/laravel-dompdf

Step 3 – Configure DOMPDF Package

In this step, registered this package in laravel application. 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 4 – Define PDF Routes

In this step, you need to visit your routes directory and open web.php file in any text editor. And define the following routes into web.php route file:

use App\Http\Controllers\PDFController;

Route::get('create-pdf-file', [PDFController::class, 'index'])

Step 5 – Create PDF Controller By Artisan Command

In this step, execute the following command on terminal/command prompt/command line to create controller file for your laravel applications; is as follow:

php artisan make:controller PDFController

Now, visit your laravel directory app/http/controllers and open PDFController.php file. And update the following code into it:

<?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 index()
    {
        $data = [
            'title' => 'Welcome to Tutsmake.com',
            'date' => date('m/d/Y')
        ];

        $pdf = PDF::loadView('testPDF', $data);

        return $pdf->download('tutsmake.pdf');
    }
}

Step 6 – Create Blade File

Now, create blade view file for generate pdf from view. So, Go to resources/views and create testPDF.blade.php and update the following code into it:

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 10 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 7 – Run Development Server

Last step, open command prompt and run the following command to start developement server:

php artisan serve

Then open your browser and hit the following url on it:

http://127.0.0.1:8000/create-pdf-file

Conclusion

that’s it, in this tutorial you have learned how to generate pdf from blade view, html using dompdf in laravel 10 apps.

Recommended Laravel Posts

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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