Laravel 10 How to Merge Two PDF Files Example

01-Jun-2023

.

Admin

Laravel 10 How to Merge Two PDF Files Example

Hello Friends,

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

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

As we know almost documents are written in 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.

In this tutorial, we will use the 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 the user will select multiple pdf files then it will return a single file with the merge.

So, let's follow a few steps and get an 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 the following step.

composer create-project laravel/laravel example-app

Step 2: Install lara-pdf-merger Package

first of all, we will install the Lara-pdf-merger composer package by following the composer command in your laravel 10 application.

composer require lynx39/lara-pdf-merger

After successfully installing the package, open the config/app.php file and add the 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 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

<?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 a new controller FileController that will manage the get and post methods of the 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 10 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 10 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 the web browser, type the given URL and view the app output:

http://localhost:8000/file

So, basically you can use it 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 10