Nov 12, 2024
0
Level 1
Can't Generate PDF File Laravel 9 (Using PHPWord)
The issue is when I tried to generate pdf then it's stuck/freeze. This issue arise after I upgrade laravel from laravel 5 to laravel 9
This is in my OfferController
public function downloadOfferLetterGenerate(ProjectUnitScheme $project_unit_scheme)
{
$scheme_folder = $project_unit_scheme->id;
$status_file = "$scheme_folder/status.json";
$status = [
'progress' => 0,
'status' => 'started',
];
Storage::disk('download')->deleteDirectory($scheme_folder);
Storage::disk('download')->put($status_file, json_encode($status));
$query = $project_unit_scheme->applications()
->where('status_id', Status::OFFERED);
$total = $query->count();
$chunk = ceil($total * 0.10);
try {
$query->chunk($chunk, function ($applications, $page) use ($scheme_folder, $status_file, $chunk, $total){
foreach ($applications as $index => $application) {
$folder_date_offer = $application->date_offer->format('Y-m-d');
$filename = "$application->project_running_no - $application->ic $application->name.pdf";
$content = app(Template::class)->generateOfferLetter($application);
Storage::disk('download')->put("$scheme_folder/$folder_date_offer/$filename", $content);
$nth = (($page - 1) * $chunk) + $index + 1;
$status = [
'status' => 'started',
'progress' => floor($nth / $total * 100),
];
Storage::disk('download')->put($status_file, json_encode($status));
}
$status = [
'status' => 'started',
'progress' => floor($page * $chunk / $total * 100),
];
Storage::disk('download')->put($status_file, json_encode($status));
});
$status = [
'status' => 'completed',
'progress' => 100,
'date_completed' => now()->format('d/m/Y h:i:s a')
];
Storage::disk('download')->put($status_file, json_encode($status));
} catch (\Throwable $e) {
$status = [
'status' => 'error',
'progress' => 0,
];
Storage::disk('download')->put($status_file, json_encode($status));
}
return $status;
}
This is in the app\Libs\Template\Template.php
public function generateOfferLetter($application)
{
if ($application->project_unit_scheme && $application->project_unit_scheme->template_surat_tawaran) {
$surat_tawaran = $application->project_unit_scheme->template_surat_tawaran;
return app(Template::class)->generateFile($surat_tawaran->realPath(), $application, $application->project_unit_scheme);
}
$offer_letter = $this->getOfferLetterTemplate($application);
return app(Template::class)->generateFileFromTemplate($offer_letter, $application);
}
Please or to participate in this conversation.