how to merge pdf files in codeigniter 4 Binaryboxtuts

How To Merge PDF Files In CodeIgniter 4

Avatar photoPosted by

Good day fellow dev, I will be showing you how to merge PDF files in CodeIgniter 4. PDF has been one of the most commonly used document formats today. Most of the web apps have functionality that generates a PDF for the invoice. receipts, reports, and others, and some web apps do pdf merging.

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-merge

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 A Controller

A Controller is the one responsible for receiving Request and returning Response. We will now create our controller, run this command to create a controller:

php spark make:controller PdfMergeController

After creating a controller add this method:

app/Controllers/PdfMergeController

<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use setasign\Fpdi\Fpdi;

class PdfMergeController extends BaseController
{
    public function index()
    {
        $files = ['file-1.pdf', 'file-2.pdf'];
        $pdf = new Fpdi();
 
        foreach ($files as $file) {
            // set the source file and get the number of pages in the document
            $pageCount =  $pdf->setSourceFile($file);
            for ($i=0; $i < $pageCount; $i++) { 
                //create a page
                $pdf->AddPage();
                //import a page then get the id and will be used in the template
                $tplId = $pdf->importPage($i+1);
                //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 5: 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-merge', 'PdfMergeController::index');

Step 6 : 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-merge