controller = $controller; } /** 生成试卷 PDF(不含答案) */ public function generateExamPdf(string $paperId): ?string { return $this->renderAndStore($paperId, includeAnswer: false, suffix: 'exam'); } /** 生成判卷 PDF(含答案与解析) */ public function generateGradingPdf(string $paperId): ?string { return $this->renderAndStore($paperId, includeAnswer: true, suffix: 'grading', useGradingView: true); } private function renderAndStore( string $paperId, bool $includeAnswer, string $suffix, bool $useGradingView = false ): ?string { // 放宽脚本执行时间,避免长耗时渲染被 PHP 全局超时打断 if (function_exists('set_time_limit')) { @set_time_limit(240); } try { $html = $this->renderHtml($paperId, $includeAnswer, $useGradingView); if (!$html) { Log::error('ExamPdfExportService: 渲染 HTML 为空', [ 'paper_id' => $paperId, 'include_answer' => $includeAnswer, 'use_grading_view' => $useGradingView, ]); return null; } $pdfBinary = $this->buildPdf($html); if (!$pdfBinary) { return null; } $path = "exams/{$paperId}_{$suffix}.pdf"; Storage::disk('public')->put($path, $pdfBinary); return URL::to(Storage::url($path)); } catch (\Throwable $e) { Log::error('ExamPdfExportService: 生成 PDF 失败', [ 'paper_id' => $paperId, 'suffix' => $suffix, 'error' => $e->getMessage(), 'exception' => get_class($e), 'trace' => $e->getTraceAsString(), ]); return null; } } private function renderHtml(string $paperId, bool $includeAnswer, bool $useGradingView): ?string { // 复用已有控制器的渲染逻辑,保证版式一致 $request = Request::create( '/admin/intelligent-exam/' . ($useGradingView ? 'grading' : 'pdf') . '/' . $paperId, 'GET', ['answer' => $includeAnswer ? 'true' : 'false'] ); $view = $useGradingView ? $this->controller->showGrading($request, $paperId) : $this->controller->show($request, $paperId); if (is_object($view) && method_exists($view, 'render')) { return $this->ensureUtf8Html($view->render()); } return null; } private function buildPdf(string $html): ?string { $tmpHtml = tempnam(sys_get_temp_dir(), 'exam_html_') . '.html'; $utf8Html = $this->ensureUtf8Html($html); file_put_contents($tmpHtml, $utf8Html); // 仅使用 Chrome 渲染,去掉 wkhtmltopdf 兜底以暴露真实问题 $chromePdf = $this->renderWithChrome($tmpHtml); @unlink($tmpHtml); return $chromePdf; } private function renderWithChrome(string $htmlPath): ?string { $tmpPdf = tempnam(sys_get_temp_dir(), 'exam_pdf_') . '.pdf'; // 固定用户目录,减少 Chrome 首次初始化开销;允许多进程并发时可按需加锁 $userDataDir = sys_get_temp_dir() . '/chrome-pdf-profile'; $chromeBinary = env('PDF_CHROME_BINARY'); if (!$chromeBinary) { $candidates = [ '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', '/usr/bin/google-chrome-stable', '/usr/bin/google-chrome', '/usr/bin/chromium-browser', '/usr/bin/chromium', ]; foreach ($candidates as $path) { if (is_file($path) && is_executable($path)) { $chromeBinary = $path; break; } } } if (!$chromeBinary) { Log::error('ExamPdfExportService: 未找到可用的 Chrome/Chromium,已停止导出', [ 'html_path' => $htmlPath, 'path_env' => env('PATH'), 'candidates_checked' => $candidates ?? [], ]); return null; } $process = new Process([ $chromeBinary, '--headless', '--disable-gpu', '--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage', '--no-zygote', '--disable-features=VizDisplayCompositor', '--disable-software-rasterizer', '--disable-extensions', '--disable-background-networking', '--disable-component-update', '--disable-client-side-phishing-detection', '--disable-default-apps', '--disable-domain-reliability', '--disable-sync', '--safebrowsing-disable-auto-update', '--no-first-run', '--no-default-browser-check', '--disable-crash-reporter', '--disable-print-preview', '--user-data-dir=' . $userDataDir, '--print-to-pdf=' . $tmpPdf, '--print-to-pdf-no-header', '--allow-file-access-from-files', 'file://' . $htmlPath, ]); $process->setTimeout(60); try { $startedAt = microtime(true); Log::info('ExamPdfExportService: Chrome 渲染启动', [ 'cmd' => $process->getCommandLine(), 'html_path' => $htmlPath, 'tmp_pdf' => $tmpPdf, 'user_data_dir' => $userDataDir, 'html_exists' => file_exists($htmlPath), 'html_size' => file_exists($htmlPath) ? filesize($htmlPath) : null, 'cwd' => $process->getWorkingDirectory(), ]); $process->start(); $pdfGenerated = false; // 轮询检测 PDF 是否生成,尽快返回,避免等待 Chrome 完整退出 $pollStart = microtime(true); $maxPollSeconds = 45; while ($process->isRunning() && (microtime(true) - $pollStart) < $maxPollSeconds) { if (file_exists($tmpPdf) && filesize($tmpPdf) > 0) { $pdfGenerated = true; Log::info('ExamPdfExportService: 发现 PDF 已生成,提前结束 Chrome', [ 'duration_sec' => round(microtime(true) - $startedAt, 3), 'tmp_pdf_size' => filesize($tmpPdf), ]); $process->stop(5, SIGKILL); break; } usleep(200_000); // 200ms } // 如果仍在运行且超过轮询窗口,则强制结束 if ($process->isRunning()) { Log::warning('ExamPdfExportService: Chrome 轮询超时,强制结束', [ 'duration_sec' => round(microtime(true) - $startedAt, 3), ]); $process->stop(5, SIGKILL); } $process->wait(); Log::info('ExamPdfExportService: Chrome 渲染完成', [ 'duration_sec' => round(microtime(true) - $startedAt, 3), 'exit_code' => $process->getExitCode(), 'tmp_pdf_exists' => file_exists($tmpPdf), 'tmp_pdf_size' => file_exists($tmpPdf) ? filesize($tmpPdf) : null, 'stderr' => $process->getErrorOutput(), 'stdout' => $process->getOutput(), 'pdf_generated_during_poll' => $pdfGenerated, ]); } catch (ProcessTimedOutException|ProcessSignaledException $e) { Log::error('ExamPdfExportService: Chrome 进程异常', [ 'cmd' => $process->getCommandLine(), 'signal' => method_exists($process, 'getTermSignal') ? $process->getTermSignal() : null, 'error' => $process->getErrorOutput(), 'output' => $process->getOutput(), 'exit_code' => $process->getExitCode(), 'exception' => $e->getMessage(), 'trace' => $e->getTraceAsString(), ]); if ($process->isRunning()) { $process->stop(5, SIGKILL); } $pdfExists = file_exists($tmpPdf); $pdfSize = $pdfExists ? filesize($tmpPdf) : null; if ($pdfExists && $pdfSize > 0) { Log::warning('ExamPdfExportService: Chrome 异常但产生了 PDF,尝试继续返回', [ 'tmp_pdf_exists' => $pdfExists, 'tmp_pdf_size' => $pdfSize, 'duration_sec' => isset($startedAt) ? round(microtime(true) - $startedAt, 3) : null, ]); $pdfBinary = file_get_contents($tmpPdf); @unlink($tmpPdf); File::deleteDirectory($userDataDir); return $pdfBinary ?: null; } @unlink($tmpPdf); File::deleteDirectory($userDataDir); return null; } catch (\Throwable $e) { Log::error('ExamPdfExportService: Chrome 调用异常', [ 'cmd' => $process->getCommandLine(), 'error' => $e->getMessage(), 'exit_code' => $process->getExitCode(), 'stderr' => $process->getErrorOutput(), 'stdout' => $process->getOutput(), 'trace' => $e->getTraceAsString(), ]); if ($process->isRunning()) { $process->stop(5, SIGKILL); } $pdfExists = file_exists($tmpPdf); $pdfSize = $pdfExists ? filesize($tmpPdf) : null; if ($pdfExists && $pdfSize > 0) { Log::warning('ExamPdfExportService: Chrome 调用异常但产生了 PDF,尝试继续返回', [ 'tmp_pdf_exists' => $pdfExists, 'tmp_pdf_size' => $pdfSize, 'duration_sec' => isset($startedAt) ? round(microtime(true) - $startedAt, 3) : null, ]); $pdfBinary = file_get_contents($tmpPdf); @unlink($tmpPdf); File::deleteDirectory($userDataDir); return $pdfBinary ?: null; } @unlink($tmpPdf); File::deleteDirectory($userDataDir); return null; } $pdfExists = file_exists($tmpPdf); $pdfSize = $pdfExists ? filesize($tmpPdf) : null; if (!$process->isSuccessful()) { if ($pdfExists && $pdfSize > 0) { Log::warning('ExamPdfExportService: Chrome 进程异常但生成了 PDF,继续使用', [ 'cmd' => implode(' ', (array) $process->getCommandLine()), 'exit_code' => $process->getExitCode(), 'error' => $process->getErrorOutput(), 'output' => $process->getOutput(), 'tmp_pdf_exists' => $pdfExists, 'tmp_pdf_size' => $pdfSize, 'html_path' => $htmlPath, 'user_data_dir' => $userDataDir, ]); } else { Log::error('ExamPdfExportService: Chrome 渲染失败', [ 'cmd' => implode(' ', (array) $process->getCommandLine()), 'exit_code' => $process->getExitCode(), 'error' => $process->getErrorOutput(), 'output' => $process->getOutput(), 'tmp_pdf_exists' => $pdfExists, 'tmp_pdf_size' => $pdfSize, 'html_path' => $htmlPath, 'user_data_dir' => $userDataDir, ]); @unlink($tmpPdf); File::deleteDirectory($userDataDir); return null; } } $pdfBinary = $pdfExists ? file_get_contents($tmpPdf) : null; @unlink($tmpPdf); File::deleteDirectory($userDataDir); return $pdfBinary ?: null; } private function ensureUtf8Html(string $html): string { $meta = ''; if (stripos($html, '') !== false) { return preg_replace('//i', "{$meta}", $html, 1); } return $meta . $html; } }