How To Add Page Numbers To PDF In Laravel 11

How To Add Page Numbers To PDF In Laravel 11

Avatar photoPosted by

Welcome to this blog, Today, I will be showing you how to add page numbers to PDF in Laravel 11. PDF has been one of the most commonly used document formats today. This tutorial is made as easy as possible for the readers to easily learn.

What is PDF? It stands for Portable Document Format. It is a file format developed by Adobe to present or display documents in a manner independent of application software, hardware, and operating systems.

Laravel is a free, open-source PHP Web Framework and intended for the development of web applications following the MVC (Model-View-Controller) architectural pattern. Laravel is designed to make developing web apps faster and easier by using built-in features.

Now we will proceed with the actual setup and code for merging PDF files. We will assume that these files are located in the /public folder of the Laravel project directory.

Prerequisite:

Step 1: Install Laravel 11

Run this command on Terminal or CMD to install Laravel via composer:

composer create-project laravel/laravel laravel-11-pdf-page-numbering

or via Laravel Installer:

laravel new laravel-11-pdf-page-numbering

Step 2: Install Packages

We will install now setasign/fpdf and setasign/fpdi packages. We will be using these packages to read and generate PDF. If you want to know more about the packages, open the link here for the setasign/fpdf and setasign/fpdi.

setasign/fpdf – This package contains a PHP class that can be used to generate PDF files using pure PHP. The F on FPDF means free: the developer may use it for any kind of usage and modify it depending on its needs. You might want to read more about FPDF on this link.

setasign/fpdi – This package contains a collection of PHP classes that allows us to read pages from existing PDF files and used them as templates for FPDF. You might want to read more about FPDF on this link.

composer require setasign/fpdf
composer require setasign/fpdi

Step 3: Create PDF Class

We will now create a PDF class, this class will extend the FPDI. Before we create this class, we will create first a folder in /app/Http which will be named Helpers.

After that we will now create the PDF class, copy the code below:

/app/Http/Helpers/PDF.php

<?php
namespace App\Http\Helpers;
use setasign\Fpdi\Fpdi;

class PDF extends FPDI
{

    function Footer()
    {
        // Position at 1.5 cm from bottom
        $this->SetY(-15);
        // Arial italic 8
        $this->SetFont('Arial','I',8);
        // Page number
        $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
    }
}

Step 4: Create A Controller

We will now create our controller, run this command to create a controller:

php artisan make:controller PdfNumberingController

After creating a controller add this method:

/app/Http/Controllers/PdfNumberingController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Helpers\PDF;

class PdfNumberingController extends Controller
{
    public function index() {
       // initiate PDF
        $pdf = new PDF();

        // set the source file
        $pageCount = $pdf->setSourceFile("file-1.pdf");
 
        $pdf->AliasNbPages();
        for ($i=1; $i <= $pageCount; $i++) { 
            //import a page then get the id and will be used in the template
            $tplId = $pdf->importPage($i);
            //create a page
            $pdf->AddPage();
            //use the template of the imporated page
            $pdf->useTemplate($tplId);
        }


        $pdf->Output();
    }
}

In this tutorial the location of the files is found on the /public folder of the Laravel project, you can use any file path on your preference to test the merging of pdf files. make sure to use a correct file path to avoid errors when merging files.

Step 5: Register Route

Now we will be registering the route for merging the PDF files. Open the file /routes/web.php and update the routes:

/routes/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PdfNumberingController;
/*
|--------------------------------------------------------------------------
| 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('/', function () {
    return view('welcome');
});

Route::get('/pdf-page-numbering', [PdfNumberingController::class, 'index']);

Step 6 : Run the Application

After finishing the steps above, you can now run your application by executing the code below:

php artisan serve

After successfully running your app, open this URL in your browser:

http://localhost:8000/pdf-page-numbering

This URL will load the numbered pages of the PDF file on the browser. So there you have now functionality that adds page numbers on PDF files.