DEV Community

Cover image for Use of DomPDF in Laravel 10
Snehal Rajeev Moon
Snehal Rajeev Moon

Posted on • Edited on

Use of DomPDF in Laravel 10

Hello Artisans,

In the world of web development it's quite common to have the need, for generating PDF documents on the fly. Thankfully Laravel, a PHP framework offers integration with various packages that make this process much simpler. One such package is domPDF, a used library that effortlessly facilitates PDF generation within Laravel. In this guide we will take you through the steps to utilize the capabilities of domPDF and enhance your Laravel applications.

What is domPDF?
DomPDF is a PHP library that allows you to effortlessly convert HTML content into PDF documents. By integrating it with Laravel you gain access to a range of possibilities, for generating PDFs with ease.

Step 1: Install Laravel Project:
If you haven't already, create a new Laravel project using Composer:

composer create-project --prefer-dist laravel/laravel dompdf-laravel
Enter fullscreen mode Exit fullscreen mode

Navigate to your project directory:

cd dompdf-laravel
Enter fullscreen mode Exit fullscreen mode

Step 2: Install domPDF Package:
Install the domPDF package using Composer:

composer require barryvdh/laravel-dompdf
Enter fullscreen mode Exit fullscreen mode

Step 3: Configure Laravel:
After installation is done, we need to register the service provider and alias by adding the following lines to config/app.php file:

'providers' => [
    // ...
    Barryvdh\DomPDF\ServiceProvider::class,
],

'aliases' => [
    // ...
    'PDF' => Barryvdh\DomPDF\Facade::class,
],
Enter fullscreen mode Exit fullscreen mode

Step 4: Publish Configuration:
Now we need to publish the configuration file to customize domPDF settings:

php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"
Enter fullscreen mode Exit fullscreen mode

Edit the config/dompdf.php file as needed.

Step 5: Create a Controller:
Generate a controller to handle PDF generation:

php artisan make:controller PDFController
Enter fullscreen mode Exit fullscreen mode

Edit the PDFController.php file to include the following:

use PDF;

class PDFController extends Controller
{
    public function generatePDF()
    {
        $data = ['title' => 'domPDF in Laravel 10'];
        $pdf = PDF::loadView('pdf.document', $data);
        return $pdf->download('document.pdf');
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 6: Create a Blade View:
Now the next step is to create a Blade view for your PDF content. For example, in resources/views/pdf/document.blade.php:

<!DOCTYPE html>
<html>
<head>
    <title>{{ $title }}</title>
</head>
<body>
    <h1>{{ $title }}</h1>
    <p>This PDF document is generated using domPDF in Laravel.</p>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Step 7: Define Routes:
Define a route to invoke the PDF generation:

Edit the routes/web.php file:

use App\Http\Controllers\PDFController;

Route::get('/generate-pdf', [PDFController::class, 'generatePDF']);
Enter fullscreen mode Exit fullscreen mode

Step 8: Test Your Application:
Run your Laravel development server using following command:

php artisan serve
Enter fullscreen mode Exit fullscreen mode

Now visit http://localhost:8000/generate-pdf in your browser to trigger the PDF generation.

Conclusion:
Great job! You've managed to incorporate the use of domPDF into your Laravel application enabling it to generate dynamic PDF documents. This fantastic duo now allows you to explore opportunities, like crafting invoices, reports or any other PDF document that your application might need. Take advantage of the extensive customization options available. Unlock the potential of domPDF, in your Laravel projects.

IF YOU LIKED THE POST, THEN YOU CAN BUY ME MY FIRST COFFEE EVER, THANKS IN ADVANCE.

Buy Me A Coffee

Happy coding!
Happy Reading!
❤️ 🦄

Top comments (3)

Collapse
 
developpeurtaf profile image
Fabrice

Good

Collapse
 
maximovj profile image
Victor J. Maximo

So great, thanks

Collapse
 
wdshahadatkhan profile image
Md. Shahadat Khan

Thanks! It is helpful