Categories
Laravel

How to Merge PDF Files using Laravel, PHP

Laravel merge pdfs

In this tutorial, you will learn how to merge multiple pdf files in your Laravel application. We gonna write a simple PHP script to merge one or more pdf files (Laravel merge pdfs), into a single file. This script will work with any version of the Laravel framework.

We gonna install the setasign\Fpdi library using composer and we’ll be using it to merge multiple pdf files into a single file.

Let’s start implementing it by installing the Laravel framework. This tutorial assumes that your computer already has PHP and Composer installed.

Step 1: Install Laravel framework with the composer.
you can create a new Laravel project by using Composer directly with the below command. After the application has been created, you may start Laravel’s local development server using the Artisan CLI’s serve command as shown below.

composer create-project laravel/laravel example-app

cd example-app

php artisan serve

Step 2: Install FPDI Package using composer

composer require setasign/fpdf
composer require setasign/fpdi

Step 3: Open the existing controller file or create a new controller with php artisan make:controller MergePDFController command and copy below methods to it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use setasign\Fpdi\Fpdi;

class MergePDFController extends Controller
{
    public function process(Request $request) {
        $file=Storage::disk('local')->put('first.pdf',file_get_contents('http://www.africau.edu/images/default/sample.pdf'));
        $file=Storage::disk('local')->put('second.pdf',file_get_contents('http://www.africau.edu/images/default/sample.pdf'));
        $files = [Storage::disk('local')->path("first.pdf"), Storage::disk('local')->path('second.pdf')];
        $this->merge($files,Storage::disk('local')->path('newtest.pdf'));
        return response()->file(Storage::disk('local')->path('newtest.pdf'));

    }

    public function merge($files, $outputPath)
    {
        $fpdi = new FPDI;
        foreach ($files as $file) {
            $filename  = $file;
            $count = $fpdi->setSourceFile($filename);
            for ($i=1; $i<=$count; $i++) {
                $template   = $fpdi->importPage($i);
                $size       = $fpdi->getTemplateSize($template);
                $fpdi->AddPage($size['orientation'], array($size['width'], $size['height']));
                $fpdi->useTemplate($template);
            }

        }
        $fpdi->Output($outputPath, 'F');
    }
}

Step 4: Define a route in web.php to test the functions.

Route::get('/merge-pdfs', [\App\Http\Controllers\MergePDFController::class,'process']);

Step5: Now start your application with php artisan serve and access it at http://localhost:8000/merge-pdfs

You can see the output files at:

By Praveen Anaparthi

11+ years of IT industry experience holding a wide range of skill sets and roles with significant work on PHP, Node.js, Python, Ruby, Docker, React.js, Microsoft Azure, Azure DevOps, Windows PowerShell, Shell script, Jenkins, MongoDB, SQL, MySQL, Apache, Nginx. etc. It is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.

2 replies on “How to Merge PDF Files using Laravel, PHP”

Leave a Reply

Your email address will not be published.