Combining Multiple Documents: If you have several PDF documents that are related or need to be presented as a single entity, merging them into one PDF file can make it easier for users to view and manage the documents.
Archiving Documents: If you have a collection of related documents that you want to archive or store, merging them into a single PDF file can make it easier to manage and retrieve the information in the future.
Step 1: Install Laravel
composer create-project laravel/laravel example-app
Step 2: Install webklex/laravel-pdfmerger Package
composer require webklex/laravel-pdfmerger
config/app.php
'providers' => [
....
Webklex\PDFMerger\Providers\PDFMergerServiceProvider::class
],
'aliases' => [
....
'PDFMerger' => Webklex\PDFMerger\Facades\PDFMergerFacade::class,
]
Step 3: Create Routes
routes/web.php
<?php
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
app/Http/Controllers/PDFController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Webklex\PDFMerger\Facades\PDFMergerFacade as PDFMerger;
class PDFController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index(): View
{
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 = PDFMerger::init();
foreach ($request->file('filenames') as $key => $value) {
$pdf->addPDF($value->getPathName(), 'all');
}
$fileName = time().'.pdf';
$pdf->merge();
$pdf->save(public_path($fileName));
}
return response()->download(public_path($fileName));
}
}
Step 5: Create View File
resources/views/mergePDF.blade.php
<html lang="en">
<head>
<title>Laravel 10 Merge Multiple PDF Files Example - webthestuff.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">
@if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Sorry!</strong> There were more problems with your HTML input.<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<h3 class="well">Laravel Merge Multiple PDF Files Example - webthestuff.com</h3>
<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>
</body>
</html>
php artisan serve
Now, Go to web browser, type the URL and see the output:
http://localhost:8000/merge-pdf