Files
webstatement/app/Jobs/CombinePdfJob.php
Daeng Deni Mardaeni d85954bdf2 feat(webstatement): tambahkan pengaturan tujuan output dan unggah ke SFTP untuk combine PDF
- Memperbarui `CombinePdfJob`:
  - Menambahkan parameter baru `outputDestination`, `branchCode`, dan `period` pada constructor untuk pengaturan tujuan output.
  - Menambahkan opsi tujuan output ke `local` atau `sftp` pada proses combine PDF.
  - Menambahkan metode baru `uploadToSftp` untuk mengunggah file PDF hasil gabungan ke SFTP.
  - Mengatur jalur unggahan SFTP ke `combine/{period}/{branchCode}/{filename}`.
  - Menambahkan log informasi terkait jalur dan status unggahan file PDF ke SFTP.

- Memperbarui `CombinePdfController`:
  - Menambahkan konfigurasi `output_destination` untuk menentukan tujuan output (`local` atau `sftp`).
  - Memperbarui pemanggilan `CombinePdfJob::dispatch` dengan parameter baru untuk konfigurasi output dan SFTP.
  - Menyesuaikan log dan respons untuk mencerminkan tujuan output yang disetel.

- Tujuan pembaruan ini:
  - Memungkinkan pengaturan flexibel tujuan penyimpanan file PDF ke lokal atau SFTP.
  - Menyediakan log yang lebih informatif terkait proses combine PDF dan unggahan SFTP.
  - Mempermudah integrasi dan pengelolaan file PDF dengan pengaturan jalur dan periodisasi yang jelas.

Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
2025-06-05 11:37:09 +07:00

139 lines
4.6 KiB
PHP

<?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 Illuminate\Support\Facades\File;
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;
protected $outputDestination;
protected $branchCode;
protected $period;
/**
* 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
* @param string $outputDestination Output destination: 'local' or 'sftp'
* @param string $branchCode Branch code for SFTP path
* @param string $period Period for SFTP path
*/
public function __construct(array $pdfFiles, string $outputPath, string $outputFilename, string $password, string $outputDestination = 'local', string $branchCode = '', string $period = '')
{
$this->pdfFiles = $pdfFiles;
$this->outputPath = $outputPath;
$this->outputFilename = $outputFilename;
$this->password = $password;
$this->outputDestination = $outputDestination;
$this->branchCode = $branchCode;
$this->period = $period;
}
/**
* 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.");
}
// Handle output destination
if ($this->outputDestination === 'sftp') {
$this->uploadToSftp($fullPath);
}
Log::info("PDFs combined successfully. Output file: {$fullPath}, Destination: {$this->outputDestination}");
} catch (Exception $e) {
Log::error("Error combining PDFs: " . $e->getMessage());
throw $e;
}
}
/**
* Upload the combined PDF to SFTP server
*
* @param string $localFilePath
*/
private function uploadToSftp(string $localFilePath): void
{
try {
// Define SFTP path: combine/{period}/{branchCode}/{filename}
$sftpPath = "combine/{$this->period}/{$this->branchCode}/{$this->outputFilename}";
// Read the local file content
$fileContent = File::get($localFilePath);
// Upload to SFTP
Storage::disk('sftpStatement')->put($sftpPath, $fileContent);
Log::info("Combined PDF uploaded to SFTP: {$sftpPath}");
// Optionally, remove the local file after successful upload
// File::delete($localFilePath);
} catch (\Exception $e) {
Log::error("Error uploading combined PDF to SFTP: {$e->getMessage()}");
throw $e;
}
}
}