Step 1: Install CodeIgniter 4
To install CodeIgniter 4 we can install it through Composer or download CodeIgniter 4 directly here:
Install via composer:
composer create-project codeigniter4/appstarter
ci-4-pdf-page-numbering
Step 2: Change CodeIgniter Environment
CodeIgniter is the default environment product, a security feature to add security when settings go wrong when it’s live. To change the environment, we will rename or copy the file env to .env. Once renamed, open the file and comment, and change the CI_ENVIRONMENT value from product 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 PDFs. 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/fpdfcomposer require setasign/fpdi
Step 4: Create a 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
“Thanks for one more wonderful informative post, Iโm a regular reader of your blog.
I genuinely appreciate your hard work. Expect more posts in the future.”
“Great content as always! Thanks a lot.”
“This is what exactly I was looking for๐
Thanks a lot buddy for sharing.,its really easy to understand and implement.”
Very detailed explanation. Thanks for sharing it.
I love this articles. Thanks for sharing.