How to Set Header and Footer in Laravel PDF?


Share:
 | Published By: Admin | Published On: Feb 12, 2022 | Category: Laravel 8 Laravel

Hi Guys,

Today, in this tutorial I will share with you something new about how to make your custom header and footer dompdf in laravel application, we will show an example of header and footer dompdf in laravel.

So, One of the prevalent issues developers have with this library is setting the header and footer of pages for the PDF file, and that is what this article is about.

Here, I will give you a full example of how to add header and footer in pdf using laravel in laravel as below so follow my all steps.

Step 1 : Install Laravel

In the first step, we need to get fresh laravel version application So let's open terminal and run bellow command to install fresh laravel project.

composer create-project --prefer-dist laravel/laravel blog
Step 2 : Install dompdf Package

first of all we will install barryvdh/laravel-dompdf composer package by following composer command in your laravel application.

composer require barryvdh/laravel-dompdf

After successfully install package, open config/app.php file and add service provider and alias.

config/app.php
'providers' => [
    ....
    Barryvdh\DomPDF\ServiceProvider::class,
],
  
'aliases' => [
    ....
    'PDF' => Barryvdh\DomPDF\Facade::class,
]
Step 3: Create 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:Create Controller

In this step,we will create a controller. Use the below command for generate controller

Path : app/Http/Controllers/PDFController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PDF;

class PDFController extends Controller
{
    /**
     * Write Your Code..
     *
     * @return string
    */
    public function generatePDF()
    {
        $data = [
            'title' => 'Welcome to Mywebtuts.com',
            'date' => date('m/d/Y')
        ];
          
        $pdf = PDF::loadView('myPDF', $data);
    
        return $pdf->download('mywebtuts.pdf');
    }
}

Step 5:Create a blade view

In this step, we will create a blade file name myPDF.blade.php bellow following path.

Path : resources/views/myPDF.blade.php
<html>
    <head>
        <style>
            /** Define the margins of your page **/
            @page {
                margin: 100px 25px;
            }

            header {
                position: fixed;
                top: -60px;
                left: 0px;
                right: 0px;
                height: 60px;

                /** Extra personal styles **/
                background-color: #deeab4;
                color: white;
                text-align: center;
                line-height: 35px;
            }

            img{
                margin-top: 8px;
            }

            footer {
                position: fixed; 
                bottom: -60px; 
                left: 0px; 
                right: 0px;
                height: 60px; 
                font-size: 20px !important;
                color: black !important;

                /** Extra personal styles **/
                background-color: #deeab4;
                text-align: center;
                line-height: 35px;
            }
        </style>
    </head>
    <body>
        <!-- Define header and footer blocks before your content -->
        <header>
            <img src="https://www.mywebtuts.com/image/logo-1.png" title="mywebtuts" width="150" height="45">
        </header>

        <footer>
            <div style="margin-top: 8px !important">Copyright © <?php echo date("Y");?> . All rights reserved.</div>
        </footer>

        <!-- Wrap the content of your PDF inside a main tag -->
        <main>
            <p style="page-break-after: always;">
                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>
            <p style="page-break-after: never;">
                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>
        </main>
    </body>
</html>
Preview

Now, we will use the php artisan serve command.

php artisan serve

Now we are ready to run our example so run bellow command to quick run.

http://localhost:8000/generate-pdf

It will help you...

Share: