Pārlūkot izejas kodu

fix: 处理换行符问题

大侠咬超人 1 nedēļu atpakaļ
vecāks
revīzija
a002802468
1 mainītis faili ar 28 papildinājumiem un 9 dzēšanām
  1. 28 9
      app/Services/ExamPdfExportService.php

+ 28 - 9
app/Services/ExamPdfExportService.php

@@ -970,21 +970,24 @@ class ExamPdfExportService
             $normalizedType = $this->normalizeQuestionType($typeRaw);
             $number = $question->question_number ?? ($idx + 1);
 
+            // 处理题干文本
+            $questionText = is_array($question->question_text)
+                ? json_encode($question->question_text, JSON_UNESCAPED_UNICODE)
+                : ($question->question_text ?? '');
+
             $payload = [
                 'question_number' => $number,
-                'question_text' => is_array($question->question_text)
-                    ? json_encode($question->question_text, JSON_UNESCAPED_UNICODE)
-                    : ($question->question_text ?? ''),
+                'question_text' => $this->formatNewlines($questionText),  // 格式化换行
                 'question_type' => $normalizedType,
                 'knowledge_point' => $kpCode,
                 'knowledge_point_name' => $kpName,
                 'score' => $question->score,
-                'answer' => $answer,  // 正确答案
-                'solution' => $solution,  // 解题思路
-                'student_answer' => $question->student_answer ?? null,  // 【新增】学生答案
-                'correct_answer' => $answer,  // 【新增】正确答案
-                'is_correct' => $question->is_correct ?? null,  // 【新增】判分结果
-                'score_obtained' => $question->score_obtained ?? null,  // 【新增】得分
+                'answer' => $this->formatNewlines($answer),  // 格式化换行
+                'solution' => $this->formatNewlines($solution),  // 格式化换行
+                'student_answer' => $this->formatNewlines($question->student_answer ?? null),  // 格式化换行
+                'correct_answer' => $this->formatNewlines($answer),  // 格式化换行
+                'is_correct' => $question->is_correct ?? null,
+                'score_obtained' => $question->score_obtained ?? null,
             ];
 
             $grouped[$normalizedType][] = $payload;
@@ -1255,6 +1258,9 @@ class ExamPdfExportService
      */
     private function inlineExternalResources(string $html): string
     {
+        // 【换行处理】将字面的 \n 转换为 <br> 标签(在 KaTeX 渲染前处理)
+        $html = str_replace(['\\n\\n', '\\n'], ['<br><br>', '<br>'], $html);
+
         // 【调试】记录HTML内容信息
         Log::warning('ExamPdfExportService: inlineExternalResources', [
             'html_length' => strlen($html),
@@ -2023,4 +2029,17 @@ class ExamPdfExportService
         // 默认返回解答题
         return 'answer';
     }
+
+    /**
+     * 格式化换行符:将字面的 \n 转换为 HTML <br> 标签
+     * 用于 PDF 渲染时正确显示换行
+     */
+    private function formatNewlines(?string $text): ?string
+    {
+        if ($text === null) {
+            return null;
+        }
+        // 先处理 \n\n(双换行),再处理 \n(单换行)
+        return str_replace(['\\n\\n', '\\n'], ['<br><br>', '<br>'], $text);
+    }
 }