In this article, we will see how to merge two pdf files in laravel 9. Here, we will learn laravel 8/9 to merge two pdf files. For this example, we will use the Laravel PDFMerger package to merge two or more pdf files in laravel 9.
You can merge multiple pdf files into a single pdf in laravel and PHP. Also, you can merge all pages, specific pages, and a range of pages using the PDFMerger package.
So, let's see the laravel 9 merges two pdf files, merge pdf in laravel 7, laravel 8, and laravel 9, and PHP merges two pdf files.
Step 1: Install Laravel 9
Step 2: Install webklex/laravel-pdfmerger Package
Step 3: Create Routes
Step 4: Create PDFMergeController
Step 5: Create Blade File
Step 6: Run Laravel 9 Application
In this step, we will install the laravel 9 application using the following command.
composer create-project laravel/laravel laravel9_pdf_merge
Now, we will install the webklex/laravel-pdfmerger package using the composer command.
composer require webklex/laravel-pdfmerger
Add the service provider to the provider's array in config/app.php
.
'providers' => [
...
Webklex\PDFMerger\Providers\PDFMergerServiceProvider::class
],
'aliases' => [
...
'PDFMerger' => Webklex\PDFMerger\Facades\PDFMergerFacade::class
]
In this step, we will add routes to the web.php file.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PDFMergeController;
/*
|--------------------------------------------------------------------------
| 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-files', [PDFMergeController::class, 'index']);
Route::post('merge-pdf-files', [PDFMergeController::class, 'store'])->name('merge.pdf.files.post');
Now, we will create a PDFMergeController.php file. So, add the following code to that file.
app/Http/Controllers/PDFMergeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Webklex\PDFMerger\Facades\PDFMergerFacade as PDFMerger;
class PDFMergeController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('merge-pdf');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'pdf_files' => 'required',
'pdf_files.*' => 'mimes:pdf'
]);
if($request->hasFile('pdf_files')){
$pdf = PDFMerger::init();
foreach ($request->file('pdf_files') as $key => $value) {
$pdf->addPDF($value->getPathName(), 'all');
}
$fileName = time().'.pdf';
$pdf->merge();
$pdf->save(public_path($fileName));
}
return response()->download(public_path($fileName));
}
}
In this step, we will create the merge-pdf.blade.php file. So, add the following code to that file.
resources/views/merge-pdf.blade.php
<html lang="en">
<head>
<title>How To Merge Two PDF Files In Laravel 9 - Websolutionstuff</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">How To Merge Two PDF Files In Laravel 9 - Websolutionstuff</h3>
<form method="post" action="{{ route('merge.pdf.files.post') }}" enctype="multipart/form-data">
{{csrf_field()}}
<input type="file" name="pdf_files[]" class="myfrm form-control" multiple="">
<button type="submit" class="btn btn-success" style="margin-top:10px">Submit</button>
</form>
</div>
</body>
</html>
Now, we will run merge two pdf files in the laravel 9 application using the following command.
php artisan serve
You might also like:
- Read Also: How To Generate PDF and Send Email In Laravel 8
- Read Also: How To Convert HTML To PDF using JavaScript
- Read Also: How Generate PDF From HTML View In Laravel
- Read Also: Laravel 9 CRUD With Image Upload Example