From 8e293c4ffb54f4c1fb154e1329d1074cc6f9de94 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Mon, 8 Jun 2026 23:30:12 +0530 Subject: [PATCH] Fix Chrome PDF corruption by clearing output buffers and adding X-Content-Type-Options header - Clear all output buffers before sending PDF binary to prevent corruption - Add X-Content-Type-Options: nosniff to force Chrome to treat response as PDF - Apply fixes to main, fallback, and ultimate fallback PDF response paths - Fixes issue where Chrome fails to open PDF while Firefox/Edge work - Critical for PDFs with Hindi/UTF-8 content where output buffering can corrupt binary data --- app/Controllers/Ipd_discharge.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/app/Controllers/Ipd_discharge.php b/app/Controllers/Ipd_discharge.php index 6acca8a..abcf49c 100644 --- a/app/Controllers/Ipd_discharge.php +++ b/app/Controllers/Ipd_discharge.php @@ -6168,12 +6168,19 @@ class Ipd_discharge extends BaseController return $mpdf->Output($fileName, Destination::STRING_RETURN); }); + + // Clear any output buffers to prevent corruption of binary PDF data + while (ob_get_level() > 0) { + ob_end_clean(); + } + return $this->response ->setHeader('Content-Type', 'application/pdf') ->setHeader('Content-Length', (string) strlen($pdfBinary)) ->setHeader('Content-Disposition', 'inline; filename="' . rawurlencode($fileName) . '"; filename*=UTF-8\'\''. rawurlencode($fileName)) ->setHeader('Cache-Control', 'private, max-age=0, must-revalidate') ->setHeader('Accept-Ranges', 'bytes') + ->setHeader('X-Content-Type-Options', 'nosniff') ->setBody($pdfBinary); } catch (\Throwable $e) { log_message('error', 'PDF generation failed for IPD {ipd}: {msg}', [ @@ -6214,12 +6221,18 @@ class Ipd_discharge extends BaseController log_message('warning', 'Discharge PDF fallback render succeeded for IPD {ipd}', ['ipd' => $ipdId]); + // Clear any output buffers to prevent corruption of binary PDF data + while (ob_get_level() > 0) { + ob_end_clean(); + } + return $this->response ->setHeader('Content-Type', 'application/pdf') ->setHeader('Content-Length', (string) strlen($pdfBinary)) ->setHeader('Content-Disposition', 'inline; filename="' . rawurlencode($fileName) . '"; filename*=UTF-8\'\''. rawurlencode($fileName)) ->setHeader('Cache-Control', 'private, max-age=0, must-revalidate') ->setHeader('Accept-Ranges', 'bytes') + ->setHeader('X-Content-Type-Options', 'nosniff') ->setBody($pdfBinary); } catch (\Throwable $fallbackError) { log_message('error', 'Discharge PDF fallback failed for IPD {ipd}: {msg}', [ @@ -6266,12 +6279,18 @@ class Ipd_discharge extends BaseController log_message('warning', 'Discharge PDF ultimate fallback render succeeded for IPD {ipd}', ['ipd' => $ipdId]); + // Clear any output buffers to prevent corruption of binary PDF data + while (ob_get_level() > 0) { + ob_end_clean(); + } + return $this->response ->setHeader('Content-Type', 'application/pdf') ->setHeader('Content-Length', (string) strlen($pdfBinary)) ->setHeader('Content-Disposition', 'inline; filename="' . rawurlencode($fileName) . '"; filename*=UTF-8\'\''. rawurlencode($fileName)) ->setHeader('Cache-Control', 'private, max-age=0, must-revalidate') ->setHeader('Accept-Ranges', 'bytes') + ->setHeader('X-Content-Type-Options', 'nosniff') ->setBody($pdfBinary); } catch (\Throwable $ultimateError) { log_message('error', 'Discharge PDF ultimate fallback failed for IPD {ipd}: {msg}', [