生成pdf

This commit is contained in:
2026-01-09 15:03:30 +08:00
parent 620ef3fe3a
commit 4a1e243726
6 changed files with 105 additions and 9 deletions
+27 -5
View File
@@ -15,6 +15,10 @@ class UsedCarController extends BaseController
protected $service; protected $service;
protected $noNeedLogin = ['downloadPdf'];
protected $noNeedHeaderData = ['downloadPdf'];
public function __construct() public function __construct()
{ {
$this->service = new UsedCarService(); $this->service = new UsedCarService();
@@ -68,11 +72,15 @@ class UsedCarController extends BaseController
} }
if (!empty($data['attachment'])){ if (!empty($data['attachment'])){
$attachments = explode(',', $data['attachment']); //$attachments = explode(',', $data['attachment']);
$attachments = json_decode($data['attachment'], true);
$ossService = new OssService(); $ossService = new OssService();
$attachment_pull = []; $attachment_pull = [];
foreach ($attachments as $attachment){ foreach ($attachments as $attachment){
$attachment_pull[] = $ossService->generatePrivateUrl($attachment); $attachment_pull[] = [
'name'=>$attachment['name'],
'src'=>$ossService->generatePrivateUrl($attachment['src'])
];
} }
$data['attachment_pull'] = $attachment_pull; $data['attachment_pull'] = $attachment_pull;
@@ -101,10 +109,24 @@ class UsedCarController extends BaseController
$images[] = $this->service->downloadImage($image); $images[] = $this->service->downloadImage($image);
} }
$data['images'] = $images; $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); $pdf = (new MpdfDriver())->exportUsedCarPdf($data);
UsedCar::where('id', $data['id'])->update(['pdf_src' => $pdf]); 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/images/*.jpg')));
array_map('unlink', glob(runtime_path('pdf/attachment/*.pdf')));
return $this->success($pdf); return $this->success($pdf);
} catch (\Throwable $e) { } catch (\Throwable $e) {
return $this->error($e->getMessage().$e->getLine().$e->getFile()); return $this->error($e->getMessage().$e->getLine().$e->getFile());
@@ -113,15 +135,15 @@ class UsedCarController extends BaseController
public function downloadPdf(Request $request){ public function downloadPdf(Request $request){
$raw = $this->service->serviceShow($request); $raw = $this->service->serviceShow($request);
$filePath = runtime_path('pdf') . $raw['pdf_src']; $filePath = runtime_path('pdf') .'/'. $raw['pdf_src'];
if (!file_exists($filePath)) { if (!file_exists($filePath)) {
return response('文件不存在', 404); return response($filePath.'文件不存在', 404);
} }
return response()->download( return response()->download(
$filePath, $filePath,
$raw['product_name'].'-'.$raw['id'].'pdf' // 下载时显示的文件名 $raw['product_name'].'-'.$raw['id'].'.pdf' // 下载时显示的文件名
); );
} }
} }
+21
View File
@@ -94,6 +94,9 @@ class UsedCarService
if(empty($data['price'])){ if(empty($data['price'])){
$data['price'] = 0; $data['price'] = 0;
} }
if(!empty($data['attachment'])){
$data['attachment'] = json_encode($data['attachment']);
}
$data['user_id'] = $request->user['id']; $data['user_id'] = $request->user['id'];
return $data; return $data;
} }
@@ -268,4 +271,22 @@ class UsedCarService
return $path; 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;
}
} }
@@ -14,7 +14,7 @@ class PdfController
public function previewFromFile(Request $request): Response 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, [ $html = PdfHelper::loadTemplate($tpl, [
'title' => '备货单', 'title' => '备货单',
+23 -3
View File
@@ -156,14 +156,34 @@ class MpdfDriver
$mpdf->WriteHTML($html); $mpdf->WriteHTML($html);
foreach ($car['attachment'] as $attachment) {
$this->appendPdf($mpdf,$attachment);
}
$pdf = $mpdf->Output('车辆详情.pdf', 'S'); $pdf = $mpdf->Output('车辆详情.pdf', 'S');
$dir = runtime_path('pdf'); $dir = runtime_path('pdf');
if (!is_dir($dir)) mkdir($dir, 0777, true); if (!is_dir($dir)) mkdir($dir, 0777, true);
$pdfName = '/invoice-' . date('YmdHis').'-'.$car['id'] . '.pdf'; $pdfName = 'invoice-' . date('YmdHis').'-'.$car['id'] . '.pdf';
$path = $dir . $pdfName; $path = $dir . '/' . $pdfName;
file_put_contents($path, $pdf); 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);
}
} }
} }
+30
View File
@@ -0,0 +1,30 @@
<?php
namespace app\process;
use app\common\model\UsedCar;
use support\Db;
use support\Log;
use Workerman\Crontab\Crontab;
class Task
{
public function onWorkerStart()
{
// 每天三点删除pdf
new Crontab('0 0 3 * * *', function(){
// echo date('Y-m-d H:i:s') . "\n";
Log::info('send follow start',[time()]);
Db::table('used_car')
->update(['pdf_src' => '']);
array_map('unlink', glob(runtime_path('pdf/*.pdf')));
Log::info('send follow end',[time()]);
});
}
}
+3
View File
@@ -60,4 +60,7 @@ return [
] ]
] ]
], ],
'task' => [
'handler' => app\process\Task::class
],
]; ];