Skip to content
  • Homepage
  • HTML
  • CSS
  • Symfony
  • PHP
  • How to
  • Contact
  • Donate

Teach Developer

Articles, Guides & Tips

How to add page numbers in PDF in CodeIgniter

Home  »  Codeigniter • How to   »   How to add page numbers in PDF in CodeIgniter
Posted on March 15, 2022July 20, 2022 5 Comments on How to add page numbers in PDF in CodeIgniter
718

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
Codeigniter, How to Tags:Codeigniter

Post navigation

Previous Post: Common Mistakes with Conditionals
Next Post: 10 Programming Habits that every Developer Should Adopt

Related Posts

  • How to Search Recently Modified Files in Linux
  • How to check if a customer is logged in to Magento 2 or not?
  • How to use events listeners and Event Subscriber in Symfony
  • How to make the div move up and down when scrolling the page with CSS?
  • How to Display Breadcrumb without Plugin in WordPress
  • How to PHP Get Max Value From Array

5 Comments on “How to add page numbers in PDF in CodeIgniter”

  1. Dorothy D. Moore says:
    July 23, 2022 at 7:05 am

    “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.”

    Reply
  2. Lucashmsilva says:
    July 24, 2022 at 6:41 am

    “Great content as always! Thanks a lot.”

    Reply
  3. Rakibul Haq says:
    July 24, 2022 at 6:46 am

    “This is what exactly I was looking for๐Ÿ™
    Thanks a lot buddy for sharing.,its really easy to understand and implement.”

    Reply
  4. Alex Xu says:
    July 24, 2022 at 6:52 am

    Very detailed explanation. Thanks for sharing it.

    Reply
  5. Imanali Mamadiev says:
    July 24, 2022 at 6:57 am

    I love this articles. Thanks for sharing.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Codeigniter (3)
  • CSS (11)
  • eCommerce (1)
  • Framework (1)
  • Git (3)
  • How to (43)
  • HTML (5)
  • JavaScript (15)
  • Jquery (7)
  • Laravel (1)
  • Linux (4)
  • Magento-2 (1)
  • Node js (4)
  • Others (2)
  • PHP (11)
  • React (13)
  • Server (1)
  • SSH (3)
  • Symfony (6)
  • Tips (16)
  • Top Tutorials (10)
  • Ubuntu (3)
  • Vue (1)
  • Wordpress (7)

Latest Posts

  • What is SSH in Linux?
  • How to Delete Files in Ubuntu Command Line
  • How to Deploy a React application on a cPanel
  • How to use events listeners and Event Subscriber in Symfony
  • How to Convert PHP CSV to JSON

WEEKLY TAGS

AJAX (1) Codeigniter (1) Javascript (11) JQuery (1) PHP (16) Programming (1) React (3) Symfony (1)

Random Post

Difference Between Git and GitHub
Important most things you should know about JSX
How to PHP Get Max Value From Array
How to Symfony Request
How to change the link color of the current page with CSS?

Quick Navigation

  • About
  • Contact
  • Privacy Policy

© Teach Developer 2021. All rights reserved