feat(webstatement): tambahkan fitur unlock PDF file dan jadwal otomatisasi harian

- Menambahkan command baru `webstatement:unlock-pdf` untuk membuka file PDF yang dilindungi password:
  - Dapat menerima parameter `directory` untuk menetapkan direktori file sumber.
  - Opsi `--password` untuk menentukan password yang digunakan dalam proses unlock, dengan default `123456`.
  - Menampilkan log proses unlock PDF dengan pesan sukses atau error.
- Membuat job baru `UnlockPdfJob` untuk menangani proses unlock PDF secara asinkron:
  - Memindai direktori utama berdasarkan struktur folder (tahun dan ID).
  - Membuka proteksi file PDF dengan menggunakan library `qpdf`.
  - Menghasilkan file PDF yang telah didekripsi di direktori yang sama dengan nama file ekstensi `.dec.pdf`.
  - Melakukan logging untuk setiap file yang berhasil diproses atau ketika terjadi error.
  - Menghindari duplikasi dengan memeriksa keberadaan file decrypted sebelumnya.
- Memperbarui `WebstatementServiceProvider`:
  - Mendaftarkan command `UnlockPdf` untuk digunakan dalam aplikasi.
  - Menambah jadwal otomatisasi harian pada pukul 09:30 untuk menjalankan command `webstatement:unlock-pdf`.
  - Logging hasil proses executables ke file `logs/unlock-pdf.log`.

Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
This commit is contained in:
Daeng Deni Mardaeni
2025-06-02 19:59:33 +07:00
parent 173b229f07
commit d326cce6e0
3 changed files with 193 additions and 1 deletions

49
app/Console/UnlockPdf.php Normal file
View File

@@ -0,0 +1,49 @@
<?php
namespace Modules\Webstatement\Console;
use Illuminate\Console\Command;
use Modules\Webstatement\Jobs\UnlockPdfJob;
use Exception;
class UnlockPdf extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'webstatement:unlock-pdf {directory} {--password=123456 : Password for PDF files}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Unlock password-protected PDF files in the specified directory';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
try {
$directory = $this->argument('directory');
$password = $this->option('password');
$this->info('Starting PDF unlock process...');
// Dispatch the job
UnlockPdfJob::dispatch($directory, $password);
$this->info('PDF unlock job has been queued.');
return 0;
} catch (Exception $e) {
$this->error('Error processing PDF unlock: ' . $e->getMessage());
return 1;
}
}
}

135
app/Jobs/UnlockPdfJob.php Normal file
View File

@@ -0,0 +1,135 @@
<?php
namespace Modules\Webstatement\Jobs;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\File;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
class UnlockPdfJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $baseDirectory;
protected $password;
/**
* Create a new job instance.
*
* @param string $baseDirectory Base directory path to scan
* @param string $password Password to unlock PDF files
*/
public function __construct(string $baseDirectory, string $password = '123456')
{
$this->baseDirectory = $baseDirectory;
$this->password = $password;
}
/**
* Execute the job.
*/
public function handle(): void
{
try {
Log::info("Starting PDF unlock process in directory: {$this->baseDirectory}");
// Check if directory exists
if (!File::isDirectory($this->baseDirectory)) {
Log::error("Directory not found: {$this->baseDirectory}");
return;
}
// Get all subdirectories (year folders like 202505)
$yearDirectories = File::directories($this->baseDirectory);
foreach ($yearDirectories as $yearDirectory) {
$this->processYearDirectory($yearDirectory);
}
Log::info("PDF unlock process completed successfully.");
} catch (Exception $e) {
Log::error("Error unlocking PDF files: " . $e->getMessage());
}
}
/**
* Process a year directory (e.g., 202505)
*
* @param string $yearDirectory Directory path to process
*/
protected function processYearDirectory(string $yearDirectory): void
{
try {
// Get all ID directories (e.g., ID0010001)
$idDirectories = File::directories($yearDirectory);
foreach ($idDirectories as $idDirectory) {
$this->processIdDirectory($idDirectory);
}
} catch (Exception $e) {
Log::error("Error processing year directory {$yearDirectory}: " . $e->getMessage());
}
}
/**
* Process an ID directory (e.g., ID0010001)
*
* @param string $idDirectory Directory path to process
*/
protected function processIdDirectory(string $idDirectory): void
{
try {
// Get all PDF files in the directory
$pdfFiles = File::glob($idDirectory . '/*.pdf');
foreach ($pdfFiles as $pdfFile) {
$this->unlockPdf($pdfFile);
}
} catch (Exception $e) {
Log::error("Error processing ID directory {$idDirectory}: " . $e->getMessage());
}
}
/**
* Unlock a password-protected PDF file
*
* @param string $pdfFilePath Path to PDF file
*/
protected function unlockPdf(string $pdfFilePath): void
{
try {
$filename = pathinfo($pdfFilePath, PATHINFO_FILENAME);
$directory = pathinfo($pdfFilePath, PATHINFO_DIRNAME);
$decryptedPdfPath = $directory . '/' . $filename . '.dec.pdf';
// Skip if the decrypted file already exists
if (File::exists($decryptedPdfPath)) {
Log::info("Decrypted file already exists: {$decryptedPdfPath}");
return;
}
// Create qpdf command
$command = ['qpdf', '--password=' . $this->password, '--decrypt', $pdfFilePath, $decryptedPdfPath];
// Execute the command
$process = new Process($command);
$process->run();
// Check if the command was successful
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
Log::info("Unlocked PDF: {$pdfFilePath} to {$decryptedPdfPath}");
} catch (Exception $e) {
Log::error("Error unlocking PDF {$pdfFilePath}: " . $e->getMessage());
}
}
}

View File

@@ -13,6 +13,7 @@ use Modules\Webstatement\Console\ProcessDailyMigration;
use Modules\Webstatement\Console\GenerateBiayakartuCommand; use Modules\Webstatement\Console\GenerateBiayakartuCommand;
use Modules\Webstatement\Jobs\UpdateAtmCardBranchCurrencyJob; use Modules\Webstatement\Jobs\UpdateAtmCardBranchCurrencyJob;
use Modules\Webstatement\Console\GenerateBiayaKartuCsvCommand; use Modules\Webstatement\Console\GenerateBiayaKartuCsvCommand;
use Modules\Webstatement\Console\UnlockPdf;
class WebstatementServiceProvider extends ServiceProvider class WebstatementServiceProvider extends ServiceProvider
{ {
@@ -60,7 +61,8 @@ class WebstatementServiceProvider extends ServiceProvider
ProcessDailyMigration::class, ProcessDailyMigration::class,
ExportDailyStatements::class, ExportDailyStatements::class,
CombinePdf::class, CombinePdf::class,
ConvertHtmlToPdf::class ConvertHtmlToPdf::class,
UnlockPdf::class,
]); ]);
} }
@@ -112,6 +114,12 @@ class WebstatementServiceProvider extends ServiceProvider
->dailyAt('09:30') ->dailyAt('09:30')
->withoutOverlapping() ->withoutOverlapping()
->appendOutputTo(storage_path('logs/convert-html-to-pdf.log')); ->appendOutputTo(storage_path('logs/convert-html-to-pdf.log'));
// Unlock PDF
$schedule->command('webstatement:unlock-pdf')
->dailyAt('09:30')
->withoutOverlapping()
->appendOutputTo(storage_path('logs/unlock-pdf.log'));
} }
/** /**