Laravel – merge multiple pdf files example

Laravel – merge multiple pdf files example

In this post we will give you information about Laravel – merge multiple pdf files example. Hear we will give you detail about Laravel – merge multiple pdf files exampleAnd how to use it also give you demo for it if it is necessary.

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' => [

....

LynX39LaraPdfMergerPdfMergerServiceProvider::class,

],

'aliases' => [

....

'PdfMerger' => LynX39LaraPdfMergerFacadesPdfMerger::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');

Also see:Example of unionAll in Query Builder Laravel

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 AppHttpControllers;

use IlluminateHttpRequest;

class FileController extends Controller

{

/**

* Show the application dashboard.

*

* @return IlluminateHttpResponse

*/

public function create()

{

return view('create');

}

/**

* Show the application dashboard.

*

* @return IlluminateHttpResponse

*/

public function store(Request $request)

{

$this->validate($request, [

'filenames' => 'required',

'filenames.*' => 'mimes:pdf'

]);

if($request->hasFile('filenames')){

$pdf = new LynX39LaraPdfMergerPdfManage;

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 >

@if (count($errors) > 0)

<div >

<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 >Laravel - merge multiple pdf files example</h3>

<form method="post" action="{{url('file')}}" enctype="multipart/form-data">

{{csrf_field()}}

<input type="file" name="filenames[]" multiple="">

<button type="submit" 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:

Also see:Laravel 5.7 Import Export Excel to database Example

$pdf = new LynX39LaraPdfMergerPdfManage;

$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…

Hope this code and post will helped you for implement Laravel – merge multiple pdf files example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  3  =  8

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US