diff --git a/app/Controllers/Opd.php b/app/Controllers/Opd.php index e2c8a9b..118af28 100644 --- a/app/Controllers/Opd.php +++ b/app/Controllers/Opd.php @@ -2408,7 +2408,7 @@ class Opd extends BaseController } $resolvedPaperWidthMm = $format === 'LETTER' ? 215.9 : ($format === 'A5' ? 148.0 : ($format === 'CUSTOM' ? (float) $customWidthMm : 210.0)); - $html = $this->applyOpdLongContentPaginationGuard($html, min(12.0, max(5.0, (float) $marginTop)), $resolvedPaperWidthMm); + $html = $this->applyOpdLongContentPaginationGuard($html, (float) $marginTop, $resolvedPaperWidthMm); $html = mpdf_normalize_font_weight_css($html); if ($rawHeaderHtml !== '') { @@ -4742,8 +4742,18 @@ class Opd extends BaseController $widthMm = max(30.0, min($paperWidthMm - $leftMm, $dim['width'])); $rightMarginMm = max(0.0, $paperWidthMm - $leftMm - $widthMm); - // Top clearance spacer height on Page 1 (letterhead offset minus page top margin) - $spacerHeightMm = max(0.0, $dim['top'] - $pageMarginTopMm); + // Check existing @page margin-top in document + $existingPageTopMm = 0.0; + if (preg_match_all('/@page\s*\{[^}]*?margin-top\s*:\s*([0-9\.]+)\s*(mm|cm|px|in|pt)?/i', $html, $allTm, PREG_SET_ORDER)) { + $lastTm = end($allTm); + $existingPageTopMm = $convertMm((float) $lastTm[1], $lastTm[2] ?? 'mm', 297.0); + } + if ($existingPageTopMm <= 0.0 && $pageMarginTopMm > 0.0) { + $existingPageTopMm = $pageMarginTopMm; + } + + // Top clearance spacer height on Page 1 (only the delta needed to reach the template's target top) + $spacerHeightMm = max(0.0, $dim['top'] - $existingPageTopMm); // In-place update margin-left and margin-right in any existing @page blocks if (preg_match('/@page\s*\{/i', $html)) { @@ -4765,15 +4775,17 @@ class Opd extends BaseController // Insert top clearance spacer right before .RxPlace so Page 1 reserves space for letterhead, // but subsequent pages start cleanly at the top of the continuation sheet - $topSpacer = '