How to Add Password Protection for PDF File in Laravel?
Hi,
Now, let's see an example of how to set a password in pdf laravel. I would like to share with you password protect pdf in laravel. This article will give you a simple example of laravel pdf password protection. you can understand the concept of how to add a password in the pdf file laravel.
In this tutorial, I will show you how to add password protection in pdf file using laravel. we will use php-pdftk github composer package to protect the pdf file. php-pdftk provide setPassword(), setUserPassword() and passwordEncryption() method that can help to set password.
You can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version. so let's follow bellow tutorial.
Preview:
Step 1: Install Laravel
I am going to explain step by step from scratch so, we need to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run bellow command:
composer create-project --prefer-dist laravel/laravel blog
Step 2: Install php-pdftk Package
first of all we will install mikehaertl/php-pdftk composer package by following composer command in your laravel application.
composer require mikehaertl/php-pdftk
you must need to install pdftk in your system. If you are using ubuntu then you can install pdftk by following command:
sudo apt install pdftk
Step 3: Add Route
In this is step we need to create routes for items listing. so open your "routes/web.php" file and add following route.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PDFController;
/*
|--------------------------------------------------------------------------
| 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('download-pdf', [PDFController::class, 'downloadPDF']);
Step 4: Add Controller
Here,we require to create new controller PDFController that will manage downloadPDF method of route.
Make sure you have created "files" folder in public directory and put "itsolutionstuff.pdf" dummy pdf file, so we are using that existing pdf file here.
So let's put bellow code.
app/Http/Controllers/PDFController.php
<?php
namespace App\Http\Controllers;
use mikehaertl\pdftk\Pdf;
class PDFController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function downloadPDF()
{
$filePath = public_path('files/itsolutionstuff.pdf');
$pdf = new Pdf($filePath);
$password = '123456';
$userPassword = '123456a';
$result = $pdf->allow('AllFeatures')
->setPassword($password)
->setUserPassword($userPassword)
->passwordEncryption(128)
->saveAs($filePath);
if ($result === false) {
$error = $pdf->getError();
}
return response()->download($filePath);
}
}
Run Laravel App:
All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:
php artisan serve
Now, Go to your web browser, type the given URL and view the app output:
http://localhost:8000/download-pdf
Now we are ready to run this example and check it...
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. 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, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Send Email with Multiple Attachment Example
- Laravel Two Factor Authentication using Email Tutorial
- Laravel Convert PDF to Image Example
- How to Send Mail using Mailjet in Laravel?
- Laravel 8 PDF | Laravel 8 Generate PDF File using DomPDF
- Laravel Datatables Export to PDF File Example
- Laravel Create PDF File with Image Example
- How to Generate PDF with Graph in Laravel?