pdfFiles = $pdfFiles; $this->outputPath = $outputPath; $this->outputFilename = $outputFilename; $this->password = $password; } /** * Execute the job. */ public function handle(): void { try { // Initialize the PDF merger $merger = PDFMerger::init(); // Add each PDF file to the merger foreach ($this->pdfFiles as $pdfFile) { if (file_exists($pdfFile)) { $merger->addPDF($pdfFile, 'all'); } else { Log::warning("PDF file not found: {$pdfFile}"); } } // Make sure the output directory exists if (!file_exists($this->outputPath)) { mkdir($this->outputPath, 0755, true); } // Merge the PDFs $merger->merge(); // Save the merged PDF $fullPath = $this->outputPath . '/' . $this->outputFilename; $merger->save($fullPath); // Apply password protection if password is provided if (!empty($this->password)) { $tempPath = $this->outputPath . '/temp_' . $this->outputFilename; // Rename the original file to a temporary name rename($fullPath, $tempPath); // Apply password protection and save to the original filename PDFPasswordProtect::encrypt($tempPath, $fullPath, $this->password); // Remove the temporary file if (file_exists($tempPath)) { unlink($tempPath); } Log::info("PDF password protection applied successfully."); } Log::info("PDFs combined successfully. Output file: {$fullPath}"); } catch (Exception $e) { Log::error("Error combining PDFs: " . $e->getMessage()); throw $e; } } }