I want to show you today how to use the setasign/fpdi package in Laravel How to Merge Multiple PDF Files. I will provide a basic example of merging PDF files in applications for laravel 5, laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application.
As far as we are aware, nearly all documents are composed in PDF format. Therefore, you may need to combine one PDF file rather than several PDF files in order to send an email or fax. You can use this tutorial to combine numerous PDF files into one PDF file if you need to. You can use the following tutorial to make a PDF file:.
Installing the setasign/fpdi} and
setasign/fpdf} composer packages and creating one example are the goals of this tutorial. Additionally, two routes—GET and POST—will be created. Next, we’ll combine one blade file and one controller file to create one. The user will receive a single file with merging when they select multiple PDF files.
So let’s do a few actions to obtain a simple example.
Steps for Using Laravel 11’s How to Merge Multiple PDF Files?
- Step 1: Install Laravel 11
- Step 2: Install Packages
- Step 3: Create Routes
- Step 4: Create Controller
- Step 5: Create Blade File
- Run Laravel App:
Step 1: Install Laravel 11
Since we are beginning from scratch, the first thing we need to do is execute the command below to obtain a brand-new Laravel 11 version application. Thus, launch the command prompt or terminal and type the following:
composer create-project laravel/laravel PDFAPP
Step 2: Install Packages
The setasign/fpdi and setasign/fpdf composer packages will first be installed by running the composer command in your Laravel application.
composer require setasign/fpdf
composer require setasign/fpdi
Step 3: Create Routes
We must construct routes for the display form in this phase. Therefore add the following route to your “routes/web.php” file by opening it.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PDFController;
Route::get('merge-pdf', [PDFController::class, 'index']);
Route::post('merge-pdf', [PDFController::class, 'store'])->name('merge.pdf.post');
Step 4: Create Controller
In this case, a new PDFController controller must be created to handle the route’s get and post methods. So let’s insert the code below.
app/Http/Controllers/PDFController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use setasign\Fpdi\Fpdi;
class PDFController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('mergePDF');
}
/**
* 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 Fpdi();
foreach ($request->file('filenames') as $key => $value) {
$pageCount = $pdf->setSourceFile($value->getPathName());
for ($i=0; $i AddPage();
//import a page then get the id and will be used in the template
$tplId = $pdf->importPage($i+1);
//use the template of the imporated page
$pdf->useTemplate($tplId);
}
}
return response($pdf->Output())
->header('Content-Type', 'application/pdf');
}
}
}
Step 5: Create Blade File
The final step is to build the mergePDF.blade.php file (resources/views/mergePDF.blade.php) for the PDF file layout and add the following code:
resources/views/mergePDF.blade.php
<html lang="en">
<head>
<title>Laravel 11 Merge Multiple PDF Files Example - ItSolutionStuff.com</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="card mt-5">
<h3 class="card-header p-3">Laravel 11 Merge Multiple PDF Files Example - ItSolutionStuff.com</h3>
<div class="card-body">
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form method="post" action="{{ route('merge.pdf.post') }}" 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>
</div>
</div>
</body>
</html>
Run Laravel App:
After completing all the necessary steps, you must execute the command below and press Enter to launch the Laravel application:
php artisan serve
Right now, Enter the provided URL into your web browser to see the app’s output:
http://localhost:8000/merge-pdf