How to Merge Multiple PDF Files in Single File

Hello Artisan, today I'll show you how to merge multiple pdf file in a single pdf file. We'll use a package which will easier our job. Let's see how we can use it in our application.

Note: Tested on Laravel 8.75.

Table of Contents

  1. Install Package
  2. Create and Setup Controller
  3. Create Route
  4. Setup Blade Template
  5. Output

Install Package

To install the package we just need to fire the below command

composer require setasign/fpdf
composer require setasign/fpdi

And that's it, we've successfully install the package.

Create and Setup Controller

Now, we need to do some work in our controller which will process the pdf's file merge it into one. So, first create a controller using below command

Now, we need to do some work in our controller which will process the pdf's file merge it into one. So, first create a controller using below command

php artisan make:controller PdfController

It'll create controller called PdfController.php. Open the file and paste the following code


namespace App\Http\Controllers;

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

class PdfController extends Controller
    public function index()
        return view('index');

    public function store(Request $request)
            'filenames' => 'required',
            'filenames.*' => 'mimes:pdf'

        $files = [];

        foreach ($request->file('filenames') as $key => $value) {
            $files[] =$value->getPathName();

        $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->Output(public_path('/merged-pdf.pdf'), 'F');

        return back()->with('message' , 'File Merged Successfully');

Create Route

Put the below route in your web.php


Setup Blade Template

Now we need setup our blade file where we can upload pdf files and download the merged one. So create index.blade.php under resources/views and paste the following code.

    <link rel="stylesheet" href="">

    <!-- jQuery library -->
    <script src=""></script>

    <!-- Popper JS -->
    <script src=""></script>

    <!-- Latest compiled JavaScript -->
    <script src=""></script>
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">
                    <h2>Laravel 8 Merge Multiple Pdf into One</h2>
                <div class="card-body">
                    @if (session()->has('message'))
                        <div class="alert alert-success">
                            {{ session('message') }}
                    <form method="post" action="{{route('file')}}" enctype="multipart/form-data">
                        <input type="file" name="filenames[]" class="myfrm form-control" multiple>
                        <div class="text-center">
                            <button type="submit" class="btn btn-success" style="margin-top:10px">Submit</button>
                <table class="table table-bordered">
                        <th><a href="{{ asset('merged-pdf.pdf') }}" class="btn btn-primary" download="merged_pdf.pdf">Download</a></th>


Now we've completed our tutorial. It's time to show the result. Go to and you'll find the below output

Pdf Upload Form

Now upload 2/3 pdf files then you can see the below output with download option of your merged pdf file.

Download Option For Merged Pdf

That's it for today. Hope you'll enjoy through this tutorial. You can also also download this from GitHub. Thank's for reading :)