预览pdf
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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']);
|
||||
|
||||
Reference in New Issue
Block a user