Sfoglia il codice sorgente

fix: 如果多个 换行符, 最多只保留2个,防止过多的空白

大侠咬超人 6 giorni fa
parent
commit
88491dfddb
1 ha cambiato i file con 6 aggiunte e 3 eliminazioni
  1. 6 3
      app/Services/ExamPdfExportService.php

+ 6 - 3
app/Services/ExamPdfExportService.php

@@ -1362,7 +1362,9 @@ class ExamPdfExportService
             }
 
             // 【换行处理】将字面的 \n 转换为 <br> 标签(在 KaTeX 渲染后处理,避免破坏公式)
-            $html = str_replace(['\\n\\n', '\\n'], ['<br><br>', '<br>'], $html);
+            // 先替换所有 \n 为 <br>,然后合并连续的 <br>(最多保留2个)
+            $html = str_replace('\\n', '<br>', $html);
+            $html = preg_replace('/(<br>\s*){3,}/', '<br><br>', $html);  // 3个以上合并为2个
 
         } catch (\Exception $e) {
             Log::warning('ExamPdfExportService: 内联资源处理失败,保留原始HTML', [
@@ -2039,7 +2041,8 @@ class ExamPdfExportService
         if ($text === null) {
             return null;
         }
-        // 先处理 \n\n(双换行),再处理 \n(单换行)
-        return str_replace(['\\n\\n', '\\n'], ['<br><br>', '<br>'], $text);
+        // 替换 \n 为 <br>,然后合并连续的 <br>(最多保留2个)
+        $text = str_replace('\\n', '<br>', $text);
+        return preg_replace('/(<br>\s*){3,}/', '<br><br>', $text);
     }
 }