Home Laravel How to Add Password Protection for PDF File in Laravel?

How to Add Password Protection for PDF File in Laravel?

by Shahriar Sagor

Today now in this post i will show you How to Add Password Protection for PDF File in Laravel? Somw days ago i was got a mail from my bank account with an attachment of monthly statement. When i was try to open then it’s want password for open. On that time i want to know how i can Add Password Protection for PDF File in Laravel. So now, let’s see an example of how to set a password in pdf laravel.

Password Protection for PDF File

Here I would like to share with you the password protect pdf in laravel application. Now in this article i will give you a a simple example of laravel pdf with the password protection. We can understand the concept of how we can add a password in the pdf file laravel.

So now in this tutorial, I will show you how we can add the password protection in pdf file by using laravel. Here i will use php-pdftk github composer package to protect the pdf file. We know php-pdftk provide setPassword(), setUserPassword() and passwordEncryption() method on that it can help to set the password.

We can also use this example with any version laravel 6, laravel 7, laravel 8 and laravel 9 version. so let’s follow the bellow tutorial.

Step 1: Install Laravel

If you have already laravel 9 application then this step is not need ; however, if you don’t have then need to created the laravel application, then you may go ahead and need execute the below command:

composer create-project --prefer-dist laravel/laravel blog
Step 2: Install php-pdftk Package

At first of all we need to install mikehaertl/php-pdftk composer package by using the following composer command in our laravel application.

composer require mikehaertl/php-pdftk

We must need to install the pdftk in our system. If we are using the ubuntu then we can install pdftk by using the following command:

sudo apt install pdftk
Step 3: Add Route

Now in this is step we have to create the routes for the items listing. So need to open our “routes/web.php” file and then add the 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 have to create the new controller PDFController on that will manage the downloadPDF method of route.

Make sure we have to created the “files” folder in public directory and then put “codingspoint.pdf” dummy pdf file, so we are using that existing pdf file here.

So let’s put the 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/cosingspoint.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);
    }
}

Read Also: How To Implement Laravel 9 form validation ?

Thanks for read. I hope it help you. For more you can follow us on facebook

close

You may also like