diff --git a/app/Helpers/merge_pdf_helper.php b/app/Helpers/merge_pdf_helper.php index 2ae442ed..4498b9f8 100644 --- a/app/Helpers/merge_pdf_helper.php +++ b/app/Helpers/merge_pdf_helper.php @@ -122,6 +122,10 @@ if (! function_exists('merge_ticket_pdfs')) { if (! is_dir($tempDir)) { @mkdir($tempDir, 0775, true); } + $mpdfCacheDir = $tempDir . DIRECTORY_SEPARATOR . 'mpdf'; + if (! is_dir($mpdfCacheDir)) { + @mkdir($mpdfCacheDir, 0775, true); + } $mergedName = 'merged_' . $ticket_master_id . '_' . time() . '_' . bin2hex(random_bytes(5)) . '.pdf'; $mergedPath = $uploadDir . $mergedName; @@ -271,15 +275,13 @@ if (! function_exists('merge_ticket_pdf_add_image_page')) { function merge_ticket_pdf_add_image_page(Mpdf $mpdf, string $imagePath): bool { $imageInfo = @getimagesize($imagePath); - if (empty($imageInfo[0]) || empty($imageInfo[1])) { - log_message('error', "merge_ticket_pdfs | invalid image | {$imagePath}"); - return false; - } + $ext = strtolower(pathinfo($imagePath, PATHINFO_EXTENSION)); + $imageType = $ext === 'png' || ((int) ($imageInfo[2] ?? 0) === IMAGETYPE_PNG) ? 'png' : 'jpg'; - $imageWidthPx = (int) $imageInfo[0]; - $imageHeightPx = (int) $imageInfo[1]; - $imageType = ((int) ($imageInfo[2] ?? 0) === IMAGETYPE_PNG) ? 'png' : 'jpg'; - $isLandscape = $imageWidthPx > $imageHeightPx; + $imageWidthPx = (int) ($imageInfo[0] ?? 0); + $imageHeightPx = (int) ($imageInfo[1] ?? 0); + $hasSize = $imageWidthPx > 0 && $imageHeightPx > 0; + $isLandscape = $hasSize && $imageWidthPx > $imageHeightPx; $pageWidth = $isLandscape ? 297 : 210; $pageHeight = $isLandscape ? 210 : 297; @@ -287,11 +289,21 @@ if (! function_exists('merge_ticket_pdf_add_image_page')) { $availableWidth = $pageWidth - ($margin * 2); $availableHeight = $pageHeight - ($margin * 2); - $scale = min($availableWidth / $imageWidthPx, $availableHeight / $imageHeightPx); - $drawWidth = $imageWidthPx * $scale; - $drawHeight = $imageHeightPx * $scale; - $x = ($pageWidth - $drawWidth) / 2; - $y = ($pageHeight - $drawHeight) / 2; + if ($hasSize) { + $scale = min($availableWidth / $imageWidthPx, $availableHeight / $imageHeightPx); + $drawWidth = $imageWidthPx * $scale; + $drawHeight = $imageHeightPx * $scale; + $x = ($pageWidth - $drawWidth) / 2; + $y = ($pageHeight - $drawHeight) / 2; + } else { + // Some PNGs fail getimagesize(), but mPDF can still render them. + // Use a portrait A4 fallback and let mPDF calculate image height. + log_message('error', "merge_ticket_pdfs | image size unavailable, using fallback page | {$imagePath}"); + $drawWidth = $availableWidth; + $drawHeight = 0; + $x = $margin; + $y = $margin; + } $mpdf->AddPageByArray([ 'orientation' => $isLandscape ? 'L' : 'P',