Marge Multiple PDF In Laravel 9

Hello Friends,

Before we start today you need to know how to make pdf. so if you dont know how to generate PDF you click here  and if you know lets continue. Today we talking about how to marge two or more then two pdf and download.  Sometime data will short and that time one pdf its ok but when you want to marge  multiple pdf there lots of issue. below i explain step by step how you marge multiple PDF.

Step 1: Install Laravel 9

composer create-project laravel/laravel project-name

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.

Read Also :  Full Text Search in Laravel Example

config/app.php

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

routes/web.php

<?php
    
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'
        ]);
      
         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 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.

Read Also :  Laravel 8 Resource Route Controller Example

app/Http/Controllers/PDFController.php

<?php
    
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'
        ]);
      
         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 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:

resources/views/mergePDF.blade.php

<html lang="en">
<head>
  <title>Laravel 9 Merge Multiple PDF Files Example - Web-Tuts.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 - Web-Tuts.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>

Run Laravel App:

php artisan serve

 

Read Also :  How to Send Bulk Mail in Laravel 8 Using Queue

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8000/merge-pdf