From 4835d34e0e2fc87f754b777a0542b682e88438d3 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Tue, 18 Aug 2026 15:55:09 +0530 Subject: [PATCH] Add Debug HTML button to discharge template editor --- app/Controllers/Ipd_discharge.php | 62 ++++++++++++------- .../ipd_discharge/discharge_template_edit.php | 7 +++ 2 files changed, 46 insertions(+), 23 deletions(-) diff --git a/app/Controllers/Ipd_discharge.php b/app/Controllers/Ipd_discharge.php index 9111716..66b58a8 100644 --- a/app/Controllers/Ipd_discharge.php +++ b/app/Controllers/Ipd_discharge.php @@ -7250,44 +7250,60 @@ class Ipd_discharge extends BaseController $templateTokenVars = $this->buildDischargeTemplateTokenVars($panelData, $content); $templateTokenVars['TEMPLATE_NAME'] = esc($templateName); - // DEBUG MODE: Output HTML directly if requested via ?html=1 + // DEBUG MODE: show the exact HTML string WriteHTML() receives. $htmlDebugMode = (int) ($this->request->getGet('html') ?? 0) === 1; if ($htmlDebugMode) { $dbgHeaderHtml = ''; if ($withHeader) { - $dbgConfiguredHeader = $this->applyDischargeTemplateTokens( - trim((string) ($templateSettings['header_html'] ?? '')), - $templateTokenVars + $dbgHeader = $this->applyDischargeTemplateTokens( + trim((string) ($templateSettings['header_html'] ?? '')), $templateTokenVars ); - $dbgHeaderHtml = $this->sanitizeDischargePdfHtml($dbgConfiguredHeader, false); + $dbgHeaderHtml = $this->sanitizeDischargePdfHtml($dbgHeader, false); } $dbgFooterHtml = $this->sanitizeDischargePdfHtml( - $this->applyDischargeTemplateTokens(trim((string) ($templateSettings['footer_html'] ?? '')), $templateTokenVars), - false + $this->applyDischargeTemplateTokens( + trim((string) ($templateSettings['footer_html'] ?? '')), $templateTokenVars + ), false ); - $dbgPrefixHtml = ''; - if ($withHeader && $dbgHeaderHtml !== '') { - $dbgPrefixHtml .= '' . "\n" . $dbgHeaderHtml . "\n" . '' . "\n"; + $dbgHeaderCm = (float) ($templateSettings['margin_header_cm'] ?? 0.5); + $dbgFooterCm = (float) ($templateSettings['margin_footer_cm'] ?? 0.5); + $dbgPageBlock = '' . "\n"; + $dbgNamedBlocks = ''; + if ($dbgHeaderHtml !== '') { + $dbgNamedBlocks .= '' . "\n" . $dbgHeaderHtml . "\n" . '' . "\n"; } if ($dbgFooterHtml !== '') { - $dbgPrefixHtml .= '' . "\n" . $dbgFooterHtml . "\n" . '' . "\n"; + $dbgNamedBlocks .= '' . "\n" . $dbgFooterHtml . "\n" . '' . "\n"; } - $pdfHtml = $this->buildDischargePdfHtml($panelData, $renderedHtml, $withHeader, $templateName); - $fullDebugSource = ($dbgPrefixHtml !== '' ? $dbgPrefixHtml . "\n" : '') . $pdfHtml; - + $dbgCss = trim((string) ($templateSettings['template_css'] ?? '')); + $dbgBody = $this->buildDischargePdfHtml($panelData, $renderedHtml, $withHeader, $templateName); + $fullHtml = $dbgPageBlock . $dbgNamedBlocks + . ($dbgCss !== '' ? '' . "\n" : '') + . $dbgBody; + return $this->response ->setContentType('text/html') ->setBody( '' . - 'Discharge HTML Debug - IPD ' . $ipdId . '' . - '' . - '' . - '
IPD: ' . $ipdId . ' | Template: ' . esc($templateName) . - ' | header_html raw: ' . esc((string) ($templateSettings['header_html'] ?? '(empty)')) . - ' | sanitized header: ' . esc($dbgHeaderHtml) . '
' . - '
' . htmlspecialchars($fullDebugSource, ENT_QUOTES, 'UTF-8') . '
' . + 'mPDF Debug HTML – IPD ' . $ipdId . '' . + '' . + '
IPD: ' . $ipdId . + ' | Template: ' . esc($templateName) . + ' | header_html: ' . esc((string) ($templateSettings['header_html'] ?? '(empty)')) . + ' | With Header: ' . ($withHeader ? 'Yes' : 'No') . '
' . + '
' . htmlspecialchars($fullHtml, ENT_QUOTES, 'UTF-8') . '
' . '' ); } diff --git a/app/Views/ipd_discharge/discharge_template_edit.php b/app/Views/ipd_discharge/discharge_template_edit.php index d2948e3..6e49742 100644 --- a/app/Views/ipd_discharge/discharge_template_edit.php +++ b/app/Views/ipd_discharge/discharge_template_edit.php @@ -156,6 +156,7 @@ $placeholders = [ + @@ -260,6 +261,12 @@ $placeholders = [ window.open('/' + ipdId + '/1' + (editId > 0 ? '?tpl=' + editId : ''), '_blank'); }); + document.getElementById('btn_debug_html').addEventListener('click', function () { + var ipdId = parseInt(val('preview_ipd_id'), 10); + if (!ipdId) { alert('Enter IPD ID to see mPDF debug HTML.'); return; } + window.open('/' + ipdId + '/1?html=1' + (editId > 0 ? '&tpl=' + editId : ''), '_blank'); + }); + /* Placeholder click-to-copy */ document.querySelectorAll('.ph-chip').forEach(function (chip) { chip.addEventListener('click', function () {