GWM : merge pdf

This commit is contained in:
Gowtham M 2026-05-12 11:21:31 +05:30
parent 524806f0eb
commit 1d812d9a93

View File

@ -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',