Laravel 11 Merge Multiple PDF Files Example

By Hardik Savani May 8, 2024 Category : PHP Laravel

Today, i would like to share with you how to merge multiple pdf files using setasign/fpdi package in laravel. i will write simple example of merge pdf files in laravel 5, laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application.

As we know, almost every document is written in PDF format. So, if you need to send an email or fax, you might be required to merge one PDF file instead of multiple PDF files. If you need to create one PDF file from multiple PDF files, you can follow this tutorial. If you want to create a PDF file, you can follow this tutorial: Generate PDF file in Laravel.

In this tutorial, we will install `setasign/fpdi` and `setasign/fpdf` composer packages and create one example. We will also create two routes, GET and POST. Then, we will create one controller file with one blade file. When the user selects multiple PDF files, it will return a single file with merge.

So, let's follow few steps and get easy example.

laravel 11 merge pdf files

Step for How to Merge Multiple PDF Files in Laravel 11?

  • Step 1: Install Laravel 11
  • Step 2: Install Packages
  • Step 3: Create Routes
  • Step 4: Create Controller
  • Step 5: Create Blade File
  • Run Laravel App:

Step 1: Install Laravel 11

First of all, we need to get a fresh Laravel 11 version application using the command below because we are starting from scratch. So, open your terminal or command prompt and run the command below:

composer create-project laravel/laravel example-app

Step 2: Install Packages

first of all we will install setasign/fpdi and setasign/fpdf composer packages by following composer command in your laravel application.

composer require setasign/fpdf
composer require setasign/fpdi

Step 3: Create Routes

In this is step we need to create routes for display form. so open your "routes/web.php" file and add following route.

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\PDFController;
  
Route::get('merge-pdf', [PDFController::class, 'index']);
Route::post('merge-pdf', [PDFController::class, 'store'])->name('merge.pdf.post');

Step 4: Create Controller

Here,we require to create new controller PDFController that will manage get and post method of route. So let's put bellow code.

app/Http/Controllers/PDFController.php

<?php
      
namespace App\Http\Controllers;
       
use Illuminate\Http\Request;
use setasign\Fpdi\Fpdi;
    
class PDFController extends Controller
{
     
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('mergePDF');
    }
        
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {   
        $this->validate($request, [
                'filenames' => 'required',
                'filenames.*' => 'mimes:pdf'
        ]);
      
         if($request->hasFile('filenames')){
            $pdf = new Fpdi();
      
            foreach ($request->file('filenames') as $key => $value) {
                
                $pageCount =  $pdf->setSourceFile($value->getPathName());
 
                for ($i=0; $i 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 response($pdf->Output())
                    ->header('Content-Type', 'application/pdf');
  
        }
    }
     
}   

Step 5: Create Blade File

In Last step, let's create mergePDF.blade.php(resources/views/mergePDF.blade.php) for layout of pdf file and put following code:

resources/views/mergePDF.blade.php

<html lang="en">
<head>
  <title>Laravel 11 Merge Multiple PDF Files Example - ItSolutionStuff.com</title>
  <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>    
<div class="container">
    <div class="card mt-5">
        <h3 class="card-header p-3">Laravel 11 Merge Multiple PDF Files Example - ItSolutionStuff.com</h3>
        <div class="card-body">

            @if (count($errors) > 0)
            <div class="alert alert-danger">
                <ul>
                  @foreach ($errors->all() as $error)
                      <li>{{ $error }}</li>
                  @endforeach
                </ul>
            </div>
            @endif
                 
            <form method="post" action="{{ route('merge.pdf.post') }}" enctype="multipart/form-data">
                  {{csrf_field()}}
                  <input type="file" name="filenames[]" class="myfrm form-control" multiple="">
                  <button type="submit" class="btn btn-success" style="margin-top:10px">Submit</button>
            </form>  

        </div>
    </div>
</div>
</body>
</html>

Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8000/merge-pdf

Output:

I hope it can help you...

Shares