How To Merge Two PDF Files In Laravel 9

Websolutionstuff | Dec-20-2022 | Categories : Laravel

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

 

Step 1: Install Laravel 9

In this step, we will install the laravel 9 application using the following command.

composer create-project laravel/laravel laravel9_pdf_merge

 

Step 2: Install webklex/laravel-pdfmerger Package

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
]

 

 

Step 3: Create Routes

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');

 

Step 4: Create PDFMergeController

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));
    }
     
}

 

 

Step 5: Create Blade File

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>

 

Step 6: Run Laravel Application

Now, we will run merge two pdf files in the laravel 9 application using the following command.

php artisan serve

 


You might also like:

Recommended Post
Featured Post
How to Install Elasticsearch in Laravel 10
How to Install Elasticsearch i...

Hey developers! If you're diving into the world of Laravel 10 and looking to supercharge your application's...

Read More

Dec-25-2023

Line Breaks In Laravel Blade
Line Breaks In Laravel Blade

In this post, I will show you how to break lines for Textarea in laravel blade. Many times when you save...

Read More

Jul-26-2020

Laravel 9 Socialite Login with Facebook Account
Laravel 9 Socialite Login with...

In this article, we will see laravel 9 socialite login with a facebook account. Many websites provide different typ...

Read More

Apr-16-2022

Laravel 9 Pluck Method Example
Laravel 9 Pluck Method Example

In this article, we will see laravel 9 pluck method example. The pluck method retrieves all of the v...

Read More

Jul-20-2022