pdf地址

This commit is contained in:
2026-01-09 16:17:50 +08:00
parent 8f085b1cfe
commit 3cb49be499
3 changed files with 40 additions and 1 deletions
+38 -1
View File
@@ -7,6 +7,7 @@ use app\common\service\OssService;
use app\common\traits\crud\CrudControllerTrait;
use app\common\service\UsedCarService;
use app\common\utils\pdf\service\MpdfDriver;
use support\exception\BusinessException;
use support\Request;
class UsedCarController extends BaseController
@@ -134,7 +135,7 @@ class UsedCarController extends BaseController
array_map('unlink', glob(runtime_path('pdf/attachment/*.pdf')));
return $this->success($pdf);
} catch (\Throwable $e) {
return $this->error($e->getMessage().$e->getLine().$e->getFile());
return $this->error($e->getMessage());
}
}
@@ -151,4 +152,40 @@ class UsedCarController extends BaseController
$raw['product_name'].'-'.$raw['id'].'.pdf' // 下载时显示的文件名
);
}
public function getPdf(Request $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);
}
// 返回访问地址
$baseUrl = 'https://' . request()->header('host');
$url = $baseUrl . '/pdf/' . $raw['pdf_src'];
return $this->success([
'pdf_url' => $url,
'file_name' => $raw['product_name'] . '-' . $raw['id'] . '.pdf'
]);
} catch (\Throwable $e) {
return $this->error($e->getMessage());
}
}
}