feat(webstatement): tambahkan fitur penggabungan dan proteksi file PDF
- Menambahkan command baru `webstatement:combine-pdf` melalui `CombinePdf` untuk menjalankan proses penggabungan file PDF. - Proses ini mencakup penggabungan file PDF dari folder r14 dan r23 berdasarkan periode tertentu. - File PDF yang dihasilkan juga dilindungi dengan password berbasis nomor rekening. - Membuat controller `CombinePdfController` dengan fungsi utama `combinePdfs` untuk mengontrol alur penggabungan file PDF: - Mendapatkan daftar akun yang relevan. - Mengecek file dari folder r14 dan r23 untuk setiap akun. - Melakukan logging saat file tidak ditemukan atau jika terdapat error dalam proses. - Mendaftarkan job `CombinePdfJob` untuk memproses file secara async. - Menambahkan job baru `CombinePdfJob`: - Menggunakan library `PDFMerger` untuk menggabungkan file. - Terapkan proteksi password menggunakan library `PDFPasswordProtect`. - Memastikan direktori output dibuat jika belum ada. - Melakukan logging saat proses berhasil maupun saat terjadi error. - Memperbarui `WebstatementServiceProvider`: - Mendaftarkan command baru ke dalam provider. - Menambahkan penjadwalan otomatis untuk menjalankan perintah `webstatement:combine-pdf` setiap hari pada pukul 09:30. - Logging hasil eksekusi ke file log `logs/combine-pdf.log`. Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
This commit is contained in:
95
app/Jobs/CombinePdfJob.php
Normal file
95
app/Jobs/CombinePdfJob.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?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\Storage;
|
||||
use Owenoj\PDFPasswordProtect\Facade\PDFPasswordProtect;
|
||||
use Webklex\PDFMerger\Facades\PDFMergerFacade as PDFMerger;
|
||||
|
||||
class CombinePdfJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
protected $pdfFiles;
|
||||
protected $outputPath;
|
||||
protected $outputFilename;
|
||||
protected $password;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param array $pdfFiles Array of PDF file paths to combine
|
||||
* @param string $outputPath Directory where the combined PDF will be saved
|
||||
* @param string $outputFilename Filename for the combined PDF
|
||||
* @param string $password Password to protect the PDF
|
||||
*/
|
||||
public function __construct(array $pdfFiles, string $outputPath, string $outputFilename, string $password)
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user