Laravel - merge multiple pdf files example

By Hardik Savani | January 24, 2019 | | 18155 Viewer | Category : PHP Laravel


Today, i would like to share with you how to merge multiple pdf files using lara-pdf-merger package in laravel. i will write simple example of merge pdf files in laravel 5.7 application.

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. If you want to create pdf file then you can follow this tutorial: Generate PDF file in Laravel 5.7.

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: Install lara-pdf-merger Package

first of all we will install lara-pdf-merger composer package by following composer command in your laravel 5.7 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 2: 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

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

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

Step 3: Create Controller

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

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 4: Create 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 - merge multiple pdf files example</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 - merge multiple pdf files example</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>

Now we are ready to run this example and check it...

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



Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.


We are Recommending you: