tecnickcom/tc-lib-pdf-encrypt
PHP library to handle PDF encryption for TCPDF/TC-Lib-PDF: generate and apply user/owner passwords, set permissions (print/copy/modify), and manage standard PDF security settings for producing protected PDF documents.
Installation Add the package via Composer:
composer require tecnickcom/tc-lib-pdf-encrypt
Ensure your project has a vendor/autoload.php file (Laravel handles this by default).
First Use Case Encrypt a PDF file in a Laravel controller:
use Tecnick\TCPDF\TCPDF;
use Tecnick\TCPDF\TCPDFFilters;
public function encryptPdf()
{
$pdf = new TCPDF();
$pdf->AddPage();
$pdf->SetFont('helvetica', '', 12);
$pdf->Write(0, 'Hello, this is an encrypted PDF!');
// Encrypt with a password
$pdf->setEncryption(
TCPDFFilters::ENCRYPTION_AES_256,
'user_password',
'owner_password',
TCPDFFilters::PERMISSION_PRINTING_ALLOWED
);
// Output the PDF
$pdf->Output('encrypted_file.pdf', 'D');
}
Where to Look First
Tecnick\TCPDF\TCPDFFilters for encryption constants and methods like setEncryption().Storage facade to handle file storage/retrieval before/after encryption.Encrypting Existing PDFs Load an existing PDF, encrypt it, and save it:
use Illuminate\Support\Facades\Storage;
public function encryptExistingPdf($filePath, $password)
{
$pdf = TCPDF::createPDF(
TCPDF::OPTION_CUSTOM,
TCPDF::FILE,
$filePath,
true,
'P',
0,
false,
false,
600,
'L',
false,
false,
0,
false,
false,
'UTF-8',
false,
false,
'https://tcpdf.org'
);
$pdf->setEncryption(
TCPDFFilters::ENCRYPTION_AES_256,
$password,
$password,
TCPDFFilters::PERMISSIONS_ALL
);
$encryptedPath = 'encrypted_' . $filePath;
$pdf->Output($encryptedPath, 'F');
return $encryptedPath;
}
Dynamic Passwords from User Input Validate and sanitize passwords before encryption:
use Illuminate\Support\Facades\Validator;
public function encryptWithUserPassword(Request $request)
{
$validator = Validator::make($request->all(), [
'password' => 'required|string|min:8',
'file' => 'required|file|mimes:pdf'
]);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator);
}
$file = $request->file('file');
$password = $request->input('password');
// Save temp file, encrypt, then store
$tempPath = $file->store('temp');
$encryptedPath = $this->encryptExistingPdf($tempPath, $password);
Storage::move($encryptedPath, 'encrypted_pdfs/' . $encryptedPath);
return response()->download(storage_path('app/' . $encryptedPath));
}
Batch Processing
Use Laravel’s Artisan commands or queues for bulk encryption:
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class EncryptPdfJob implements ShouldQueue
{
use Dispatchable, Queueable;
public $pdfPath;
public $password;
public function __construct($pdfPath, $password)
{
$this->pdfPath = $pdfPath;
$this->password = $password;
}
public function handle()
{
$encryptedPath = app(EncryptPdfService::class)->encrypt($this->pdfPath, $this->password);
// Notify user or update DB
}
}
Pre-Generated PDFs
TCPDF in a service class to create and encrypt PDFs in one step.User Uploads
storage/app/encrypted with restricted permissions.API Endpoints
Route::post('/api/encrypt-pdf', [PdfController::class, 'encryptWithUserPassword']);
Storage::disk('local')->put() to save encrypted files.TCPDF to add default encryption settings:
class CustomTCPDF extends TCPDF
{
public function __construct()
{
parent::__construct();
$this->setEncryption(
TCPDFFilters::ENCRYPTION_AES_256,
'default_password',
'default_password',
TCPDFFilters::PERMISSIONS_ALL
);
}
}
\Log::info('PDF encrypted', ['file' => $filePath, 'user' => auth()->id()]);
Password Security
Hash facade to store hashed passwords (if needed) or enforce strong password policies:
$validator->rules(['password' => 'required|string|min:12|confirmed']);
File Permissions
storage/app/encrypted:
chmod 700 storage/app/encrypted
TCPDF Version Conflicts
composer.json:
"require": {
"tecnickcom/tc-lib-pdf-encrypt": "^1.0",
"tecnickcom/tcpdf": "^6.4"
}
Memory Limits
memory_limit..env or process files in chunks:
memory_limit = 512M
Permission Errors
setEncryption() may fail silently if permissions are misconfigured.$permissions = TCPDFFilters::PERMISSION_PRINTING_ALLOWED |
TCPDFFilters::PERMISSION_COPY_CONTENT_ALLOWED;
$pdf->setEncryption(TCPDFFilters::ENCRYPTION_AES_256, 'pass', 'pass', $permissions);
Check Encryption Status Use Adobe Acrobat’s "Properties" > "Security" to verify encryption.
TCPDF Errors Enable TCPDF error reporting:
$pdf->SetErrorAction('throw');
$pdf->SetErrorHandler(function($error) {
\Log::error('TCPDF Error: ' . $error);
});
File Corruption
TCPDF::createPDF() with TCPDF::OPTION_CUSTOM for existing files.Default Permissions Use predefined permission constants for clarity:
$pdf->setEncryption(
TCPDFFilters::ENCRYPTION_AES_256,
'pass',
'pass',
TCPDFFilters::PERMISSIONS_ALL // or specific flags
);
Password Strength Enforce minimum password length (e.g., 12 chars) to comply with security standards.
Performance For large batches, use Laravel queues to avoid timeouts:
EncryptPdfJob::dispatch($filePath, $password)->onQueue('encrypted');
Testing Mock TCPDF in unit tests:
$mockPdf = $this->createMock(TCPDF::class);
$mockPdf->method('setEncryption')->willReturn(true);
Extension Points
TCPDF methods to add custom encryption logic.illuminate.queue:failed) to handle failed jobs.How can I help you explore Laravel packages today?