Raviya Technical
Published in

Raviya Technical

Laravel with Packages | Laravel Merge Multiple PDF Files Example

Today, i would like to share with you how to merge multiple pdf files using lara-pdf-merger package in laravel. i will write simple example of merge pdf files in laravel 5, laravel 6, laravel 7, laravel 8 and laravel 9 application.

As we know almost document written on pdf. so it you need to send email or fax then you might be require to merge one pdf file instead of multiple pdf files. If you need to create one pdf file from multiple pdf files then you can follow this tutorial.

In this tutorial, we will use lara-pdf-merger composer package 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 user will select multiple pdf files then it will return single file with merge.

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

Step 1: Install Laravel

This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Install webklex/laravel-pdfmerger Package

first of all we will install webklex/laravel-pdfmerger composer package by following composer command in your laravel application.

composer require webklex/laravel-pdfmerger

After successfully install package, open config/app.php file and add service provider and alias.


'providers' => [....Webklex\PDFMerger\Providers\PDFMergerServiceProvider::class],'aliases' => [....'PDFMerger' => Webklex\PDFMerger\Facades\PDFMergerFacade::class,]

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.



use Illuminate\Support\Facades\Route;

use App\Http\Controllers\PDFController;

| 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('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.



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Webklex\PDFMerger\Facades\PDFMergerFacade as PDFMerger;

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'

$pdf = PDFMerger::init();

foreach ($request->file('filenames') as $key => $value) {
$pdf->addPDF($value->getPathName(), 'all');

$fileName = time().'.pdf';


return response()->download(public_path($fileName));


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:


<html lang="en">
<title>Laravel 9 Merge Multiple PDF Files Example - raviyatechnical</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.1/css/bootstrap.min.css" rel="stylesheet">

<div class="container">

@if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Sorry!</strong> There were more problems with your HTML input.<br><br>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>

<h3 class="well">Laravel Merge Multiple PDF Files Example - raviyatechnical</h3>

<form method="post" action="{{ route('merge.pdf.post') }}" enctype="multipart/form-data">
<input type="file" name="filenames[]" class="myfrm form-control" multiple="">
<button type="submit" class="btn btn-success" style="margin-top:10px">Submit</button>



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




PHP, Laravel And Flutter Development

Recommended from Medium

Go: Should I Use a Pointer instead of a Copy of my Struct?

Coding is a Journey not a D

SQL Injection Vulnerability In University Of Cambridge

SCQN Scanner Update

The many failures of Keebio PCB prototypes

The Most Boring Post

Building End-to-End API Testing Framework: Part 7.1

Reliable Android Testing using LinkedIn’s Test Butler Tutorial

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Raviya Technical

Raviya Technical


More from Medium

Laravel Authentication | Laravel 8 Authentication using Jetstream Example

Laravel 9 Form Validation Example

Laravel with Packages | Laravel Google ReCAPTCHA Form Validation Example

Laravel REST API | Laravel Sanctum SPA API Authentication Example