feat(webstatement): tambahkan fitur konversi file HTML ke PDF
- Menambahkan command baru `webstatement:convert-html-to-pdf` untuk melakukan konversi file HTML menjadi PDF secara otomatis: - Dapat menerima parameter `directory` untuk menentukan direktori sumber file HTML. - Menampilkan pesan sukses atau error selama proses berjalan. - Menggunakan konsep asinkron melalui job untuk meningkatkan efisiensi. - Membuat job baru `ConvertHtmlToPdfJob` untuk menangani proses konversi file: - Memproses folder yang berisi file HTML berdasarkan struktur direktori tertentu. - Mengambil semua file HTML dalam suatu folder, kemudian mengonversinya menjadi file PDF. - Menggunakan library `Barryvdh\DomPDF\Facade\Pdf` untuk konversi format HTML ke PDF. - Melakukan logging untuk setiap proses berhasil atau ketika terjadi error. - Memastikan suksesnya konversi ke direktori yang sama dengan file HTML. Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
This commit is contained in:
102
app/Jobs/ConvertHtmlToPdfJob.php
Normal file
102
app/Jobs/ConvertHtmlToPdfJob.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?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 Barryvdh\DomPDF\Facade\Pdf;
|
||||
|
||||
class ConvertHtmlToPdfJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
protected $baseDirectory;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param string $baseDirectory Base directory path to scan
|
||||
*/
|
||||
public function __construct(string $baseDirectory)
|
||||
{
|
||||
$this->baseDirectory = $baseDirectory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
try {
|
||||
Log::info("Starting HTML to PDF conversion in directory: {$this->baseDirectory}");
|
||||
|
||||
// Check if directory exists
|
||||
if (!File::isDirectory($this->baseDirectory)) {
|
||||
Log::error("Directory not found: {$this->baseDirectory}");
|
||||
return;
|
||||
}
|
||||
|
||||
// Get all subdirectories (ID folders)
|
||||
$idDirectories = File::directories($this->baseDirectory);
|
||||
|
||||
foreach ($idDirectories as $idDirectory) {
|
||||
$this->processDirectory($idDirectory);
|
||||
}
|
||||
|
||||
Log::info("HTML to PDF conversion completed successfully.");
|
||||
} catch (Exception $e) {
|
||||
Log::error("Error converting HTML to PDF: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a single ID directory
|
||||
*
|
||||
* @param string $directory Directory path to process
|
||||
*/
|
||||
protected function processDirectory(string $directory): void
|
||||
{
|
||||
try {
|
||||
$htmlFiles = File::glob($directory . '/*.html');
|
||||
|
||||
foreach ($htmlFiles as $htmlFile) {
|
||||
$this->convertHtmlToPdf($htmlFile);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
Log::error("Error processing directory {$directory}: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a single HTML file to PDF
|
||||
*
|
||||
* @param string $htmlFilePath Path to HTML file
|
||||
*/
|
||||
protected function convertHtmlToPdf(string $htmlFilePath): void
|
||||
{
|
||||
try {
|
||||
$filename = pathinfo($htmlFilePath, PATHINFO_FILENAME);
|
||||
$directory = pathinfo($htmlFilePath, PATHINFO_DIRNAME);
|
||||
$pdfFilePath = $directory . '/' . $filename . '.pdf';
|
||||
|
||||
// Read HTML content
|
||||
$htmlContent = File::get($htmlFilePath);
|
||||
|
||||
// Convert HTML to PDF
|
||||
$pdf = PDF::loadHTML($htmlContent);
|
||||
|
||||
// Save PDF file
|
||||
$pdf->save($pdfFilePath);
|
||||
|
||||
Log::info("Converted {$htmlFilePath} to {$pdfFilePath}");
|
||||
} catch (Exception $e) {
|
||||
Log::error("Error converting {$htmlFilePath} to PDF: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user