How To Merge PDF Files in Laravel 9

How To Merge PDF Files in Laravel 9

Avatar photoPosted by

Hello there, I will be showing you how to merge PDF files in Laravel 9. 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.

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.

Step 1: Install Laravel 9

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

composer create-project laravel/laravel laravel-9-pdf-merge

or via Laravel Installer:

laravel new laravel-9-pdf-merge

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

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

 php artisan make:controller PdfMergeController

After creating a controller add this method:

/app/Http/Controllers/PdfMergeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use setasign\Fpdi\Fpdi;

class PdfMergeController extends Controller
{
    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);
            }
        }

        //return the generated PDF
        return $pdf->Output();      
    }
}

make sure you will change the file path on the $files variable. 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 4: 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\PdfMergeController;
/*
|--------------------------------------------------------------------------
| 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-merge', [PdfMergeController::class, 'index']);

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

This URL will load the merged PDF files on the browser. So there you have now functionality that merges PDF files.