diff --git a/app/Controllers/Opd.php b/app/Controllers/Opd.php
index 189dacc..e2c8a9b 100644
--- a/app/Controllers/Opd.php
+++ b/app/Controllers/Opd.php
@@ -2199,7 +2199,7 @@ class Opd extends BaseController
$tokenVars = $this->buildOpdPdfTokenVars($data);
$html = $this->applyOpdPdfTemplateTokens($customHtml, $tokenVars);
$html = $this->sanitizeOpdNamedHeaderFooterReferences($html);
- $html = $this->applyOpdLongContentPaginationGuard($html);
+ $html = $this->applyOpdLongContentPaginationGuard($html, 8.0, 210.0);
$html = mpdf_normalize_font_weight_css($html);
if ($debugHtml) {
@@ -2407,7 +2407,8 @@ class Opd extends BaseController
$rawFooterHtml = '';
}
- $html = $this->applyOpdLongContentPaginationGuard($html);
+ $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 = mpdf_normalize_font_weight_css($html);
if ($rawHeaderHtml !== '') {
@@ -2759,11 +2760,23 @@ class Opd extends BaseController
$medicalHtml = '';
if (!empty($rxMeds)) {
- $medicalHtml .= '
';
+ $medCount = count($rxMeds);
+ // Idea 1: Dynamic compact styling for prescriptions with >= 6 medicines
+ // Strictly enforce readability floor: font-size must never drop below 9.5px (or 8.5px for Hindi subtext).
+ $isCompactMeds = ($medCount >= 6);
+ $tblFontSize = $isCompactMeds ? '10px' : '12px';
+ $tblCellPadding = $isCompactMeds ? '2.5px 5px' : '6px 8px';
+ $tblHeaderPadding = $isCompactMeds ? '3px 5px' : '6px 8px';
+ $tblMarginY = $isCompactMeds ? '2px' : '5px';
+ $genericFontSize = $isCompactMeds ? '8.5px' : '10px';
+ $hindiFontSize = $isCompactMeds ? '8.5px' : '11px';
+ $hindiLineHeight = $isCompactMeds ? '1.22' : '1.4';
+
+ $medicalHtml .= '';
$medicalHtml .= ''
- . '| # | '
- . 'Medicine | '
- . 'Directions | '
+ . '# | '
+ . 'Medicine | '
+ . 'Directions | '
. '
';
$whenCodeDescMap = [
@@ -2910,7 +2923,7 @@ class Opd extends BaseController
$nameHtml = '' . esc($fullMedName) . '';
if ($generic !== '') {
// Show only generic data directly below medicine name (no "Salt/Generic:" heading label)
- $nameHtml .= '' . esc($generic) . '
';
+ $nameHtml .= '' . esc($generic) . '
';
}
// 2) English Directions & Local Language (Hindi) Directions
@@ -3028,11 +3041,11 @@ class Opd extends BaseController
$localDirectionsText = !empty($dirLocalParts) ? implode(' | ', array_values(array_unique($dirLocalParts))) : '';
$medicalHtml .= ''
- . '| ' . $i . ' | '
- . '' . $nameHtml . ' | '
- . ''
+ . ' | ' . $i . ' | '
+ . '' . $nameHtml . ' | '
+ . ''
. ' ' . esc($directionsText) . ' '
- . ($localDirectionsText !== '' ? ('' . esc($localDirectionsText) . ' ') : '')
+ . ($localDirectionsText !== '' ? ('' . esc($localDirectionsText) . ' ') : '')
. ' | '
. '
';
}
@@ -3938,9 +3951,16 @@ class Opd extends BaseController
}
}
+ $rxMeds = is_array($data['rx_medicines'] ?? null) ? $data['rx_medicines'] : [];
+ $isCompactMeds = count($rxMeds) >= 6;
+
$headingStyleMode = (string) ($this->opdPlaceholderRenderOptions['heading_style'] ?? 'bold');
$lineGap = (float) ($this->opdPlaceholderRenderOptions['line_gap'] ?? 1.6);
$blockGapMm = (float) ($this->opdPlaceholderRenderOptions['block_gap_mm'] ?? 2.8);
+ if ($isCompactMeds) {
+ $lineGap = min($lineGap, 1.35);
+ $blockGapMm = min($blockGapMm, 1.5);
+ }
$lineGapCss = number_format($lineGap, 2, '.', '');
$blockGapCss = number_format($blockGapMm, 2, '.', '');
@@ -4382,8 +4402,10 @@ class Opd extends BaseController
$tokens['morbidities_block'] = $formatBlock('Co-Morbidities', (string) ($tokens['morbidities'] ?? ''));
$medicalHtml = trim((string) ($tokens['medical'] ?? ''));
+ $rxTitleFontSize = $isCompactMeds ? '18px' : '24px';
+ $rxTitleMargin = $isCompactMeds ? '3px' : '8px';
$tokens['Rx'] = $medicalHtml !== ''
- ? $blockGapHtml . '' . $blockGapHtml
+ ? $blockGapHtml . '' . $blockGapHtml
: '';
$tokens['rx'] = $tokens['Rx'];
// {{medical}} used directly in templates: wrap with the same top/bottom margin.
@@ -4649,7 +4671,7 @@ class Opd extends BaseController
return $html;
}
- private function applyOpdLongContentPaginationGuard(string $html): string
+ private function applyOpdLongContentPaginationGuard(string $html, float $pageMarginTopMm = 10.0, float $paperWidthMm = 210.0): string
{
if ($html === '') {
return '';
@@ -4664,28 +4686,127 @@ class Opd extends BaseController
$textLength = strlen((string) $plainText);
$lineBreakCount = substr_count(strtolower($html), '
1800 || $lineBreakCount > 45 || $rowCount > 18;
+
+ // Trigger pagination guard when content threatens single-page overflow:
+ // >= 8 rows, >= 18 line breaks, or text length > 900
+ $looksLong = $rowCount >= 8 || $lineBreakCount >= 18 || $textLength > 900;
if (! $looksLong) {
return $html;
}
- $guardCss = '';
- return $guardCss . "\n" . $html;
+ return $html . $guardCss;
}
/**