Laravel 9 How to Merge Two PDF Files Example

Apr 16, 2022

.

Admin

Laravel 9 How to Merge Two PDF Files Example

Hello Friends,

This simple article demonstrates of laravel 9 how To merge two PDF files example. We will use how to merge two PDF files in laravel 9. This tutorial will give you simple example of merge two PDF files. I explained simply about pdf.

This example is focused on merge multiple pdf files laravel. step by step explain laravel 9 merge pdf files. you will learn dompdf merge pdf laravel. i would like to show you laravel merge pdf files.

As we know almost document written on pdf. so it you need to send email or fax then you might be require 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.

In this tutorial, we will use lara-pdf-merger composer package and create one example. we will also create two routes GET and POST. then we will create one controller file with one blade file. When user will select multiple pdf files then it will return single file with merge.

So, let's follow few steps and get easy example.

Step 1: Download Laravel


Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app

Step 2: Install lara-pdf-merger Package

first of all we will install lara-pdf-merger composer package by following composer command in your laravel 9 application.

composer require lynx39/lara-pdf-merger

After successfully install package, open 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 3: Add Route

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

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\FileController;

/*

|--------------------------------------------------------------------------

| 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('file',[FileController::class,'create']);

Route::post('file',[FileController::class,'store']);

Step 4: Add Controller

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

php artisan make:controller FileController

app/Http/Controllers/FileController.php

<?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 5: Add Blade File

In Last step, let's create create.blade.php(resources/views/create.blade.php) for layout of pdf file and put following code:

resources/views/create.blade.php

<html lang="en">

<head>

<title>Laravel 9 How To Merge Two PDF Files Example - NiceSnipets.com</title>

<script src="jquery/1.9.1/jquery.js"></script>

<link rel="stylesheet" href="3.3.6/css/bootstrap.min.css">

</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 9 How To Merge Two PDF Files Example - NiceSnipets.com</h3>

<form method="post" action="{{url('file')}}" 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:

All steps have been done, now you have to type the given command and hit enter to run the laravel app:

php artisan serve

Now, you have to open web browser, type the given URL and view the app output:

http://localhost:8000/file

So, basically you can use this way:

$pdf = new \LynX39\LaraPdfMerger\PdfManage;

$pdf->addPDF(public_path('/upload/1547633948.pdf'), 'all');

$pdf->addPDF(public_path('/upload/test.pdf'), 'all');

$pdf->merge('file', public_path('/upload/created.pdf'), 'P');

dd('done');

I hope it can help you...

#Laravel 9