| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- <?php
- namespace App\Http\Controllers;
- use App\Services\ExamPdfExportService;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Storage;
- class QuestionPreviewController extends Controller
- {
- /**
- * 显示预览工具页面
- */
- public function index()
- {
- return view('tools.question-preview');
- }
- /**
- * 生成 PDF 预览
- */
- public function generatePdf(Request $request)
- {
- $request->validate([
- 'stem' => 'required|string',
- 'options' => 'nullable|array',
- 'answer' => 'nullable|string',
- 'solution' => 'nullable|string',
- ]);
- try {
- $service = app(ExamPdfExportService::class);
- // 过滤空选项
- $options = array_filter($request->input('options', []), fn($v) => !empty($v));
- $pdfPath = $service->generatePreviewPdf(
- stem: $request->input('stem'),
- options: $options,
- answer: $request->input('answer'),
- solution: $request->input('solution')
- );
- // 返回可访问的 URL
- $url = Storage::disk('public')->url($pdfPath);
- return response()->json([
- 'success' => true,
- 'url' => $url,
- ]);
- } catch (\Exception $e) {
- return response()->json([
- 'success' => false,
- 'error' => $e->getMessage(),
- ], 500);
- }
- }
- }
|