From 56e7575c6f32fd11a57a0bd05241cb2b15726622 Mon Sep 17 00:00:00 2001 From: "WIN-C27C5MSVTPO\\16961" <1696136552@qq.com> Date: Thu, 15 Jan 2026 11:42:02 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A2=84=E8=A7=88pdf?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/controller/UsedCarController.php | 37 +++++++++++++++++++++ config/route.php | 1 + 2 files changed, 38 insertions(+) diff --git a/app/common/controller/UsedCarController.php b/app/common/controller/UsedCarController.php index b8d4603..2434f8b 100644 --- a/app/common/controller/UsedCarController.php +++ b/app/common/controller/UsedCarController.php @@ -409,4 +409,41 @@ class UsedCarController extends BaseController } } + /** + * 预览pdf + */ + function previewPdf($request) { + try { + $raw = $this->service->serviceShow($request); + $filePath = runtime_path('pdf') . '/' . $raw['pdf_src']; + + if (empty($raw['pdf_src']) || !file_exists($filePath)) { + throw new BusinessException('文件不存在'); + } + + // 确保 public/pdf 存在 + $publicDir = public_path('pdf'); + if (!is_dir($publicDir)) { + mkdir($publicDir, 0755, true); + } + + // 拷贝到 public 目录 + $publicFile = $publicDir . '/' . $raw['pdf_src']; + if (!file_exists($publicFile)) { + copy($filePath, $publicFile); + } + + $path = public_path('pdf/' . basename($raw['pdf_src'])); + + return response(file_get_contents($path), 200, [ + 'Content-Type' => 'application/pdf', + 'Access-Control-Allow-Origin' => '*', // 或指定前端域名 + 'Access-Control-Allow-Methods' => 'GET, OPTIONS', + 'Access-Control-Allow-Headers' => '*', + ]); + } catch (\Throwable $e) { + return $this->error($e->getMessage()); + } + } + } \ No newline at end of file diff --git a/config/route.php b/config/route.php index f26b74f..9748ad1 100644 --- a/config/route.php +++ b/config/route.php @@ -60,6 +60,7 @@ Route::group('/common', function () { Route::any('/produce/pdf', [app\common\controller\UsedCarController::class, 'producePdf']); Route::any('/download/pdf', [app\common\controller\UsedCarController::class, 'downloadPdf']); Route::any('/pdf', [app\common\controller\UsedCarController::class, 'getPdf']); + Route::any('/preview/pdf', [app\common\controller\UsedCarController::class, 'previewPdf']); Route::any('/show', [app\common\controller\UsedCarController::class, 'show']); Route::any('/store', [app\common\controller\UsedCarController::class, 'store']); Route::any('/update', [app\common\controller\UsedCarController::class, 'update']);