GWM : merge pdf
This commit is contained in:
parent
524806f0eb
commit
1d812d9a93
@ -122,6 +122,10 @@ if (! function_exists('merge_ticket_pdfs')) {
|
|||||||
if (! is_dir($tempDir)) {
|
if (! is_dir($tempDir)) {
|
||||||
@mkdir($tempDir, 0775, true);
|
@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';
|
$mergedName = 'merged_' . $ticket_master_id . '_' . time() . '_' . bin2hex(random_bytes(5)) . '.pdf';
|
||||||
$mergedPath = $uploadDir . $mergedName;
|
$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
|
function merge_ticket_pdf_add_image_page(Mpdf $mpdf, string $imagePath): bool
|
||||||
{
|
{
|
||||||
$imageInfo = @getimagesize($imagePath);
|
$imageInfo = @getimagesize($imagePath);
|
||||||
if (empty($imageInfo[0]) || empty($imageInfo[1])) {
|
$ext = strtolower(pathinfo($imagePath, PATHINFO_EXTENSION));
|
||||||
log_message('error', "merge_ticket_pdfs | invalid image | {$imagePath}");
|
$imageType = $ext === 'png' || ((int) ($imageInfo[2] ?? 0) === IMAGETYPE_PNG) ? 'png' : 'jpg';
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$imageWidthPx = (int) $imageInfo[0];
|
$imageWidthPx = (int) ($imageInfo[0] ?? 0);
|
||||||
$imageHeightPx = (int) $imageInfo[1];
|
$imageHeightPx = (int) ($imageInfo[1] ?? 0);
|
||||||
$imageType = ((int) ($imageInfo[2] ?? 0) === IMAGETYPE_PNG) ? 'png' : 'jpg';
|
$hasSize = $imageWidthPx > 0 && $imageHeightPx > 0;
|
||||||
$isLandscape = $imageWidthPx > $imageHeightPx;
|
$isLandscape = $hasSize && $imageWidthPx > $imageHeightPx;
|
||||||
|
|
||||||
$pageWidth = $isLandscape ? 297 : 210;
|
$pageWidth = $isLandscape ? 297 : 210;
|
||||||
$pageHeight = $isLandscape ? 210 : 297;
|
$pageHeight = $isLandscape ? 210 : 297;
|
||||||
@ -287,11 +289,21 @@ if (! function_exists('merge_ticket_pdf_add_image_page')) {
|
|||||||
|
|
||||||
$availableWidth = $pageWidth - ($margin * 2);
|
$availableWidth = $pageWidth - ($margin * 2);
|
||||||
$availableHeight = $pageHeight - ($margin * 2);
|
$availableHeight = $pageHeight - ($margin * 2);
|
||||||
$scale = min($availableWidth / $imageWidthPx, $availableHeight / $imageHeightPx);
|
if ($hasSize) {
|
||||||
$drawWidth = $imageWidthPx * $scale;
|
$scale = min($availableWidth / $imageWidthPx, $availableHeight / $imageHeightPx);
|
||||||
$drawHeight = $imageHeightPx * $scale;
|
$drawWidth = $imageWidthPx * $scale;
|
||||||
$x = ($pageWidth - $drawWidth) / 2;
|
$drawHeight = $imageHeightPx * $scale;
|
||||||
$y = ($pageHeight - $drawHeight) / 2;
|
$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([
|
$mpdf->AddPageByArray([
|
||||||
'orientation' => $isLandscape ? 'L' : 'P',
|
'orientation' => $isLandscape ? 'L' : 'P',
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user