How to Add Header and Footer in Dompdf laravel | websolutioncode.com
How to Add Header and Footer in Dompdf laravel | websolutioncode.com

How to Add Header and Footer in Dompdf laravel

Creating PDFs with customized headers and footers is a common requirement in web applications. Laravel, a popular PHP framework, offers a simple and efficient solution for generating PDFs using the Laravel PDF package. This tutorial will illustrate how to integrate personalized headers and footers into a PDF in Laravel, leveraging the well-known Laravel PDF package, Dompdf.

Prerequisites:

Before we start, make sure you have Laravel installed, and you’ve set up a new project. Additionally, you’ll need to install the Dompdf package, which can be done using Composer.

composer require barryvdh/laravel-dompdf

Step 1: Configure Laravel Dompdf Package:

Once the package is installed, open your config/app.php file and add the service provider and facade:

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

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

Run the following command to publish the configuration file:

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

Step 2: Create a PDF Controller:

Generate a new controller for handling PDFs:

php artisan make:controller PdfController

Open the generated PdfController.php file and add the following code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PDF;

class PdfController extends Controller
{
    public function generatePdf()
    {
        $pdf = PDF::loadView('pdf.template');
        return $pdf->stream('document.pdf');
    }
}

Step 3: Create a Blade View for the PDF:

Create a new Blade view file resources/views/pdf/template.blade.php and add the content of your PDF:

<!DOCTYPE html>
<html>
<head>
    <title>My PDF</title>
</head>
<body>
    <!-- Your PDF content here -->

    <!-- Header -->
    <div style="text-align: center; padding: 10px; background-color: #f2f2f2;">
        <h1>My PDF Header</h1>
    </div>

    <!-- Footer -->
    <div style="text-align: center; padding: 10px; background-color: #f2f2f2;">
        <p>Page {{ $page }} of {{ $pages }}</p>
    </div>
</body>
</html>

Step 4: Update the Controller for Header and Footer:

Modify the generatePdf method in PdfController.php to pass the page information to the view:

public function generatePdf()
{
    $pdf = PDF::loadView('pdf.template');
    $pdf->getDomPDF()->set_option("isHtml5ParserEnabled", true);
    $pdf->getDomPDF()->set_option("isPhpEnabled", true);
    $pdf->setOptions(['isHtml5ParserEnabled' => true, 'isPhpEnabled' => true]);
    
    $pdf->setOption('header-html', view('pdf.header'));
    $pdf->setOption('footer-html', view('pdf.footer'));
    
    return $pdf->stream('document.pdf');
}

Step 5: Create Header and Footer Blade Views:

Create two new Blade view files: resources/views/pdf/header.blade.php and resources/views/pdf/footer.blade.php. Add the content for your header and footer in these files.

Header (header.blade.php):

<!DOCTYPE html>
<html>
<head>
    <title>Header</title>
</head>
<body>
    <div style="text-align: center; padding: 10px; background-color: #f2f2f2;">
        <h1>My PDF Header</h1>
    </div>
</body>
</html>

Footer (footer.blade.php):

<!DOCTYPE html>
<html>
<head>
    <title>Footer</title>
</head>
<body>
    <div style="text-align: center; padding: 10px; background-color: #f2f2f2;">
        <p>Page {{ $page }} of {{ $pages }}</p>
    </div>
</body>
</html>

Conclusion:

Congratulations! You have successfully added a custom header and footer to your PDF in Laravel using the Dompdf package. This simple yet powerful solution allows you to customize the appearance of your PDF documents with ease. Feel free to adjust the styling and content of the header and footer to meet your specific requirements. Happy coding!

Leave a Reply