生成pdf
This commit is contained in:
@@ -15,6 +15,10 @@ class UsedCarController extends BaseController
|
||||
|
||||
protected $service;
|
||||
|
||||
protected $noNeedLogin = ['downloadPdf'];
|
||||
|
||||
protected $noNeedHeaderData = ['downloadPdf'];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->service = new UsedCarService();
|
||||
@@ -68,11 +72,15 @@ class UsedCarController extends BaseController
|
||||
|
||||
}
|
||||
if (!empty($data['attachment'])){
|
||||
$attachments = explode(',', $data['attachment']);
|
||||
//$attachments = explode(',', $data['attachment']);
|
||||
$attachments = json_decode($data['attachment'], true);
|
||||
$ossService = new OssService();
|
||||
$attachment_pull = [];
|
||||
foreach ($attachments as $attachment){
|
||||
$attachment_pull[] = $ossService->generatePrivateUrl($attachment);
|
||||
$attachment_pull[] = [
|
||||
'name'=>$attachment['name'],
|
||||
'src'=>$ossService->generatePrivateUrl($attachment['src'])
|
||||
];
|
||||
}
|
||||
$data['attachment_pull'] = $attachment_pull;
|
||||
|
||||
@@ -101,10 +109,24 @@ class UsedCarController extends BaseController
|
||||
$images[] = $this->service->downloadImage($image);
|
||||
}
|
||||
$data['images'] = $images;
|
||||
|
||||
$attachments = [];
|
||||
foreach ($data['attachment_pull'] as $attachment){
|
||||
// 方法1:使用 pathinfo
|
||||
$path = parse_url($attachment['src'], PHP_URL_PATH); // 获取路径部分
|
||||
$extension = pathinfo($path, PATHINFO_EXTENSION); // 获取扩展名
|
||||
|
||||
if (strtolower($extension) === 'pdf') { //如果是pdf则下载到本地
|
||||
$attachments[] = $this->service->downloadAttachment($attachment['src']);
|
||||
}
|
||||
}
|
||||
$data['attachment'] = $attachments;
|
||||
|
||||
$pdf = (new MpdfDriver())->exportUsedCarPdf($data);
|
||||
UsedCar::where('id', $data['id'])->update(['pdf_src' => $pdf]);
|
||||
|
||||
array_map('unlink', glob(runtime_path('pdf/images/*.jpg')));
|
||||
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());
|
||||
@@ -113,15 +135,15 @@ class UsedCarController extends BaseController
|
||||
|
||||
public function downloadPdf(Request $request){
|
||||
$raw = $this->service->serviceShow($request);
|
||||
$filePath = runtime_path('pdf') . $raw['pdf_src'];
|
||||
$filePath = runtime_path('pdf') .'/'. $raw['pdf_src'];
|
||||
|
||||
if (!file_exists($filePath)) {
|
||||
return response('文件不存在', 404);
|
||||
return response($filePath.'文件不存在', 404);
|
||||
}
|
||||
|
||||
return response()->download(
|
||||
$filePath,
|
||||
$raw['product_name'].'-'.$raw['id'].'pdf' // 下载时显示的文件名
|
||||
$raw['product_name'].'-'.$raw['id'].'.pdf' // 下载时显示的文件名
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user