Merging Multiple PDF File In Single File Example In Laravel

Today, I would like to share with you how to merge multiple pdf files using the lara-pdf-merger package in laravel. I will write a simple example of merging pdf files in laravel 5, laravel 6, laravel 7 and laravel 8 applications.

As we know almost all documents are written on pdf. so if you need to send an email or fax then you might be required 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. 

May we need to merge multiple PDF files in Laravel. So I am here to help you so that you can merge multiple PDF files. From this example, you will learn, when the user will select multiple pdf files then it will return a single file with the merge.

 

Step 1: Install Package 

Before using merging pdf package lynx39/lara-pdf-merger, we need to install it by the following command:

composer require lynx39/lara-pdf-merger

 

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

config/app.php

'providers' => [
	....
	LynX39\LaraPdfMerger\PdfMergerServiceProvider::class,
],
  
'aliases' => [
	....
	'PdfMerger' => LynX39\LaraPdfMerger\Facades\PdfMerger::class,
]

 

Step 2: Add Routes

In this second step, we need to create routes for the display form. so open your "routes/web.php" file and add the following route.

routes/web.php

Route::get('file','FileController@create');
Route::post('file','FileController@store');

 

Step 3: Add Controller

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

app/Http/Controllers/FileController.php

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'
        ]);
    
         if($request->hasFile('filenames')){
            $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:  Add Blade File

In the last step, we have to create create.blade.php for the layout of a pdf file and put the following code:

resources/views/create.blade.php

 

Read also: How to Install Imagick to Convert PDF File to Image in Laravel

 

Hope this merging pdf in laravel tutorial will help you.

 

#laravel #laravel-8x