Laravel - merge multiple pdf files example

Admin   PHP   1045  2020-08-11 00:14:22

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.7 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. If you want to create pdf file then you can follow this tutorial: Generate PDF file in Laravel 5.7.

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 lara-pdf-merger Package

first of all we will install lara-pdf-merger composer package by following composer command in your laravel 5.7 application.

composer require lynx39/lara-pdf-merger

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


'providers' => [




'aliases' => [


'PdfMerger' => LynX39\LaraPdfMerger\Facades\PdfMerger::class,


Step 2: 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.


Route::get('file','[email protected]');

Route::post('file','[email protected]');

Step 3: Create Controller

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



namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FileController extends Controller



* Show the application dashboard.


* @return \Illuminate\Http\Response


public function create()


return view('create');



* Show the application dashboard.


* @return \Illuminate\Http\Response


public function store(Request $request)


$this->validate($request, [

'filenames' => 'required',

'filenames.*' => 'mimes:pdf'



$pdf = new \LynX39\LaraPdfMerger\PdfManage;

foreach ($request->file('filenames') as $key => $value) {

$pdf->addPDF($value->getPathName(), 'all');


$input['file_name'] = time().'.pdf';

$pdf->merge('file', public_path($input['file_name']), 'P');


return response()->download(public_path($input['file_name']));



Step 4: Create Blade File

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


<html lang="en">


<title>Laravel - merge multiple pdf files example</title>

<script src="jquery/1.9.1/jquery.js"></script>

<link rel="stylesheet" href="3.3.6/css/bootstrap.min.css" target="_blank" rel="nofollow" >



<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</h3>

<form method="post" action="{{url('file')}}" 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>





Now we are ready to run this example and check it...

So, basically you can use this way:

$pdf = new \LynX39\LaraPdfMerger\PdfManage;

$pdf->addPDF(public_path('/upload/1547633948.pdf'), 'all');

$pdf->addPDF(public_path('/upload/test.pdf'), 'all');

$pdf->merge('file', public_path('/upload/created.pdf'), 'P');


I hope it can help you...