DomPDF In Laravel: How To Generate PDF Files Step by Step

Spread the love

Generating PDF files in Laravel can be a crucial feature for many web applications. Whether you need to create invoices, reports, or any other documents, DomPDF is a powerful library that can help you achieve this seamlessly. In this guide, we will walk you through the steps to generate PDF files using DomPDF in Laravel.

Introduction to DomPDF In Laravel

DomPDF is a PHP library that helps to convert HTML and CSS to PDF documents. Using DomPDF in Laravel, you can generate high-quality PDF files with ease. This article will guide you through each step to integrate DomPDF in your Laravel project and generate PDF files.

Also, Check out: How to Send Email in Laravel Using Gmail Account: A Beginner’s Guide with Step-by-Step Example

Setting Up Laravel Project

First, ensure you have Laravel installed on your system. If not, you can set up a new Laravel project by running the following command:

composer create-project --prefer-dist laravel/laravel dompdf-example

Navigate to your project directory:

cd dompdf-example

Installing DomPDF In Laravel

Next, you need to install the DomPDF package. Run the following command to install it via Composer:

composer require barryvdh/laravel-dompdf

Configuring DomPDF In Laravel

After installing the package, you need to add the service provider to your config/app.php file. Add the following line to the providers array:

Barryvdh\DomPDF\ServiceProvider::class,

Optionally, you can add the alias to the aliases array:

'PDF' => Barryvdh\DomPDF\Facade::class,

Publish the configuration file using the following command:

php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"

Creating a Route and Controller

Now, create a route and controller to handle PDF generation. Add a new route in routes/web.php:

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

Next, create the controller using Artisan:

php artisan make:controller PDFController

Designing the PDF Template

Create a new Blade template for the PDF content. For example, create a file named invoice.blade.php in the resources/views directory:

<!DOCTYPE html>
<html>
<head>
    <title>Invoice</title>
    <style>
        body { font-family: Arial, sans-serif; }
        .header { text-align: center; margin-bottom: 50px; }
        .footer { text-align: center; position: fixed; bottom: 0; width: 100%; }
        .content { margin: 0 auto; width: 80%; }
        table { width: 100%; border-collapse: collapse; margin-bottom: 20px; }
        table, th, td { border: 1px solid black; padding: 10px; text-align: left; }
    </style>
</head>
<body>
    <div class="header">
        <h1>Invoice</h1>
    </div>
    <div class="content">
        <table>
            <thead>
                <tr>
                    <th>Item</th>
                    <th>Quantity</th>
                    <th>Price</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Product 1</td>
                    <td>1</td>
                    <td>$10.00</td>
                </tr>
                <tr>
                    <td>Product 2</td>
                    <td>2</td>
                    <td>$20.00</td>
                </tr>
            </tbody>
        </table>
    </div>
    <div class="footer">
        <p>Generated on {{ date('Y-m-d') }}</p>
    </div>
</body>
</html>

Generating PDF in Controller

In your PDFController, add the logic to generate the PDF. Open app/Http/Controllers/PDFController.php and update it as follows:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PDF;

class PDFController extends Controller
{
    public function generatePDF()
    {
        $data = ['title' => 'Invoice'];
        $pdf = PDF::loadView('invoice', $data);

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

Testing the PDF Generation

Now, you can test your PDF generation by accessing the route in your browser:

http://your-app-url/generate-pdf

If everything is set up correctly, you should be prompted to download a PDF file named invoice.pdf.

Also, Check out: How to use Multiple Laravel sail project with Custom Domain

Conclusion

In this article, we have learned how to generate PDF files using DomPDF in Laravel. By following the steps outlined, you can easily integrate PDF generation into your Laravel application. DomPDF in Laravel provides a robust solution for creating high-quality PDF documents, enhancing the functionality of your web applications.

Leave a Comment