Contents
Today, I will be showing you how to add page numbers to PDF in CodeIgniter 4. 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.
We will be using CodeIgniter 4 on this tutorial. CodeIgniter is one of the most powerful PHP frameworks. It is an open-source web framework that is used for rapid web development. It is popular for its exceptional performance, small footprint, requires nearly zero configuration, thorough documentation and many more. If you want to learn more about CodeIgniter just visit their documentation here.
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 CodeIgniter project directory.
Step 1: Install CodeIgniter 4
For us to install CodeIgniter 4 we can install via composer or directly download CodeIgniter 4 here:
Install via composer:
composer create-project codeigniter4/appstarter ci-4-pdf-page-numbering
Step 2: Change CodeIgniter Environment
The default environment of CodeIgniter is production, it is a safety feature to add security in case settings are messed up when it goes live. For us to change the environment we will rename or copy the file env to .env. Once it is renamed, open the file and uncomment and change the CI_ENVIRONMENT value from production to development.
.env
CI_ENVIRONMENT = development
Step 3: 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 4: Create PDF Class
We will now create a PDF class, this class will extend the FPDI. We will create this in this directory app\Libraries.
Add these codes to the newly created PDF class:
app\Libraries\PDF.php
<?php
namespace App\Libraries;
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 5: Create A Controller
We will now create our controller, run this command to create a controller:
php spark make:controller PdfNumberingController
After creating a controller add this method:
app/Controllers/PdfNumberingController
<?php
namespace App\Controllers;
use App\Controllers\BaseController;
use App\Libraries\PDF;
class PdfNumberingController extends BaseController
{
public function index()
{
// initiate PDF
$pdf = new PDF();
// set the source file
$pageCount = $pdf->setSourceFile("file-2.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);
}
//display the generated PDF
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
readfile($pdf->Output());
}
}
Step 6: Register Route
Now we will be registering the route for merging the PDF files. Open the file /app/Config/Routes.php and update the routes:
app/Config/Routes.php
$routes->get('/pdf-page-numbering', 'PdfNumberingController::index');
Step 7 : Run the Application
Now that we have completed the steps above we will now run the app. To run the app, execute this command:
php spark serve
After successfully running your app, open this URL in your browser:
http://localhost:8080/pdf-page-numbering