From 4a1e24372671495d135c12a6a4f0c5a604dcc34d Mon Sep 17 00:00:00 2001 From: "WIN-C27C5MSVTPO\\16961" <1696136552@qq.com> Date: Fri, 9 Jan 2026 15:03:30 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=9F=E6=88=90pdf?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/controller/UsedCarController.php | 32 ++++++++++++++++--- app/common/service/UsedCarService.php | 21 ++++++++++++ .../utils/pdf/controller/PdfController.php | 2 +- app/common/utils/pdf/service/MpdfDriver.php | 26 +++++++++++++-- app/process/Task.php | 30 +++++++++++++++++ config/process.php | 3 ++ 6 files changed, 105 insertions(+), 9 deletions(-) create mode 100644 app/process/Task.php diff --git a/app/common/controller/UsedCarController.php b/app/common/controller/UsedCarController.php index c6e3f42..8831601 100644 --- a/app/common/controller/UsedCarController.php +++ b/app/common/controller/UsedCarController.php @@ -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' // 下载时显示的文件名 ); } } \ No newline at end of file diff --git a/app/common/service/UsedCarService.php b/app/common/service/UsedCarService.php index cdf6614..ca4ee77 100644 --- a/app/common/service/UsedCarService.php +++ b/app/common/service/UsedCarService.php @@ -94,6 +94,9 @@ class UsedCarService if(empty($data['price'])){ $data['price'] = 0; } + if(!empty($data['attachment'])){ + $data['attachment'] = json_encode($data['attachment']); + } $data['user_id'] = $request->user['id']; return $data; } @@ -268,4 +271,22 @@ class UsedCarService return $path; } + + function downloadAttachment(string $url): string + { + $dir = runtime_path('pdf/attachment/'); + if (!is_dir($dir)) { + mkdir($dir, 0755, true); + } + + $filename = md5($url) . '.pdf'; + $path = $dir . $filename; + + if (!file_exists($path)) { + $img = file_get_contents($url); + file_put_contents($path, $img); + } + + return $path; + } } \ No newline at end of file diff --git a/app/common/utils/pdf/controller/PdfController.php b/app/common/utils/pdf/controller/PdfController.php index b647628..ef40ad9 100644 --- a/app/common/utils/pdf/controller/PdfController.php +++ b/app/common/utils/pdf/controller/PdfController.php @@ -14,7 +14,7 @@ class PdfController public function previewFromFile(Request $request): Response { // 你的模板路径(示例) - $tpl = base_path('resource/templates/stock-order.html'); + $tpl = base_path('resource/templates/usedCar.html'); // 可选:把 {{变量}} 替换 $html = PdfHelper::loadTemplate($tpl, [ 'title' => '备货单', diff --git a/app/common/utils/pdf/service/MpdfDriver.php b/app/common/utils/pdf/service/MpdfDriver.php index 2e9b6f8..c303693 100644 --- a/app/common/utils/pdf/service/MpdfDriver.php +++ b/app/common/utils/pdf/service/MpdfDriver.php @@ -156,14 +156,34 @@ class MpdfDriver $mpdf->WriteHTML($html); + foreach ($car['attachment'] as $attachment) { + $this->appendPdf($mpdf,$attachment); + } + $pdf = $mpdf->Output('车辆详情.pdf', 'S'); $dir = runtime_path('pdf'); if (!is_dir($dir)) mkdir($dir, 0777, true); - $pdfName = '/invoice-' . date('YmdHis').'-'.$car['id'] . '.pdf'; - $path = $dir . $pdfName; + $pdfName = 'invoice-' . date('YmdHis').'-'.$car['id'] . '.pdf'; + $path = $dir . '/' . $pdfName; file_put_contents($path, $pdf); - return $path; + return $pdfName; + } + + public function appendPdf(Mpdf $mpdf, string $pdfPath) + { + $pageCount = $mpdf->SetSourceFile($pdfPath); + for ($i = 1; $i <= $pageCount; $i++) { + $tplId = $mpdf->ImportPage($i); + $size = $mpdf->getTemplateSize($tplId); + + $mpdf->AddPageByArray([ + 'orientation' => $size['orientation'], + 'sheet-size' => [$size['width'], $size['height']] + ]); + + $mpdf->UseTemplate($tplId); + } } } \ No newline at end of file diff --git a/app/process/Task.php b/app/process/Task.php new file mode 100644 index 0000000..5acd138 --- /dev/null +++ b/app/process/Task.php @@ -0,0 +1,30 @@ +update(['pdf_src' => '']); + + array_map('unlink', glob(runtime_path('pdf/*.pdf'))); + + Log::info('send follow end',[time()]); + }); + + } + +} \ No newline at end of file diff --git a/config/process.php b/config/process.php index f404f22..05b250f 100644 --- a/config/process.php +++ b/config/process.php @@ -60,4 +60,7 @@ return [ ] ] ], + 'task' => [ + 'handler' => app\process\Task::class + ], ];