This example focuses on laravel merge multiple pdf files. Explain in detail how to use Laravel 8 to merge PDF files. You will learn how to use dompdf merge pdf laravel. I'd like to demonstrate how to use Laravel to merge PDF files.
As we all know, almost all documents are written in PDF format. So, if you need to send an email or fax, you may need to merge one pdf file rather than multiple pdf files. If you need to create a single PDF file from multiple PDF files, use this tutorial.
Table of Content
In this tutorial, we will create one example using the lara-pdf-merger composer package. We'll also add two new routes, GET and POST. Then we'll combine one controller file with one blade file to make one controller file. When a user selects multiple PDF files, it returns a single file that has been merged.
So, let's go through a few steps to get a simple example.
Step 1 : Install lara-pdf-merger Package
First, we will install the lara-pdf-merger composer package in your Laravel 8 application by running the composer command.
composer require lynx39/lara-pdf-merger
After the package has been successfully installed, open the config/app.php
file and add the service provider and alias.
'providers' => [
LynX39\LaraPdfMerger\PdfMergerServiceProvider::class,
],
'aliases' => [
'PdfMerger' => LynX39\LaraPdfMerger\Facades\PdfMerger::class,
]
Step 2 : Create Routes
This step requires us to create routes for the display form. So, open your "routes/web.php
" file and add the route listed below.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FileController;
Route::get('file',[FileController::class,'create']);
Route::post('file',[FileController::class,'store']);
Step 3 : Create Controller
In this case, we'll need to create a new controller FileController
to handle the route's get and post methods. So, let's put the code below.
<?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
Finally, let's create create.blade.php (resources/views/create.blade.php
) for the layout of the pdf file and include the following code:
<html lang="en">
<head>
<title>Laravel 8 How To Merge Two PDF Files Example | codesolutionstuff.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 8 How To Merge Two PDF Files Example | codesolutionstuff.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>
We are all now ready to run and test this example.
$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 you will like the content and it will help you to learn Laravel 8 How To Merge Two PDF Files Example
If you like this content, do share.