作者 刘锟

update

  1 +<?php
  2 +
  3 +namespace App\Http\Controllers\Api;
  4 +
  5 +use App\Http\Controllers\Controller;
  6 +use BaconQrCode\Renderer\ImageRenderer;
  7 +use BaconQrCode\Renderer\RendererStyle\RendererStyle;
  8 +use BaconQrCode\Writer;
  9 +use karmabunny\BaconBackends\GdImageBackEnd;
  10 +use setasign\Fpdi\Fpdi;
  11 +use SimpleSoftwareIO\QrCode\Facades\QrCode;
  12 +
  13 +class ToolController extends Controller
  14 +{
  15 + /**
  16 + * @param array $data
  17 + * @param string $message
  18 + * @param int $status
  19 + * @return string
  20 + */
  21 + protected function success($data = [], $message = 'success', $status = 200)
  22 + {
  23 + $array = compact('status', 'message', 'data');
  24 + return json_encode($array, JSON_UNESCAPED_UNICODE);
  25 + }
  26 +
  27 + /**
  28 + * @param int $status
  29 + * @param string $message
  30 + * @param array $data
  31 + * @return string
  32 + */
  33 + protected function error($message = 'error', $status = 400, $data = [])
  34 + {
  35 + $array = compact('status', 'message', 'data');
  36 + return json_encode($array, JSON_UNESCAPED_UNICODE);
  37 + }
  38 +
  39 + public function addQrToPdf()
  40 + {
  41 + $sourceFile = public_path('original.pdf');
  42 + $qrText = 'https://www.bing.com';
  43 +
  44 + // 生成二维码图片
  45 + $qrImage = public_path('qrcode_temp.png');
  46 + $renderer = new ImageRenderer(
  47 + new RendererStyle(300, 0),
  48 + new GdImageBackEnd() // 使用 GD 后端
  49 + );
  50 + $writer = new Writer($renderer);
  51 +
  52 + // 生成 PNG 图片数据
  53 + $writer->writeFile($qrText, $qrImage);
  54 +
  55 + $pdf = new Fpdi();
  56 +
  57 + $pageCount = $pdf->setSourceFile($sourceFile);
  58 +
  59 + for ($i = 1; $i <= $pageCount; $i++) {
  60 +
  61 + $tpl = $pdf->importPage($i);
  62 + $size = $pdf->getTemplateSize($tpl);
  63 +
  64 + $pdf->AddPage($size['orientation'], [$size['width'], $size['height']]);
  65 + $pdf->useTemplate($tpl);
  66 +
  67 + // ✅ 只在第一页添加二维码
  68 + if ($i === 1) {
  69 +
  70 + $qrWidth = 30; // 二维码宽度(单位:mm)
  71 + $margin = 5; // 距离边缘间距(mm)
  72 +
  73 + $x = $size['width'] - $qrWidth - $margin;
  74 + $y = $margin;
  75 +
  76 + $pdf->Image($qrImage, $x, $y, $qrWidth);
  77 +
  78 + $text = 'Scan to verify the original MTC on official website';
  79 +
  80 + // 设置字体(必须设置,否则不显示)
  81 + $pdf->SetFont('Arial', '', 8);
  82 +
  83 + // 计算文字位置
  84 + $textY = $y + $qrWidth + 2; // 二维码下方2mm
  85 +
  86 + // 设置光标位置
  87 + $pdf->SetXY($x - 1, $textY);
  88 +
  89 + // 固定宽度,自动换行
  90 + $pdf->MultiCell($qrWidth + 10, 4, $text, 0, 'L');
  91 + }
  92 + }
  93 +
  94 + $outputFile = public_path('output.pdf');
  95 + $pdf->Output('F', $outputFile);
  96 +
  97 + // 删除临时二维码图片
  98 + unlink($qrImage);
  99 +
  100 + return $this->success(['pdf_file' => $outputFile]);
  101 + }
  102 +}
@@ -6,19 +6,23 @@ @@ -6,19 +6,23 @@
6 "license": "MIT", 6 "license": "MIT",
7 "require": { 7 "require": {
8 "php": "^7.3|^8.0", 8 "php": "^7.3|^8.0",
  9 + "ext-curl": "*",
9 "ext-json": "*", 10 "ext-json": "*",
  11 + "ext-zip": "*",
  12 + "ext-zlib": "*",
10 "fruitcake/laravel-cors": "^2.0", 13 "fruitcake/laravel-cors": "^2.0",
11 "guzzlehttp/guzzle": "^7.0.1", 14 "guzzlehttp/guzzle": "^7.0.1",
12 "jenssegers/agent": "^2.6", 15 "jenssegers/agent": "^2.6",
  16 + "karmabunny/bacon-backends": "^1.0",
13 "laravel/framework": "^8.75", 17 "laravel/framework": "^8.75",
14 "laravel/sanctum": "^2.11", 18 "laravel/sanctum": "^2.11",
15 "laravel/tinker": "^2.5", 19 "laravel/tinker": "^2.5",
16 "qcloud/cos-sdk-v5": "^2.6", 20 "qcloud/cos-sdk-v5": "^2.6",
  21 + "setasign/fpdf": "^1.8",
  22 + "setasign/fpdi": "^2.6",
  23 + "simplesoftwareio/simple-qrcode": "^4.2",
17 "voku/portable-utf8": "^6.0", 24 "voku/portable-utf8": "^6.0",
18 - "voku/simple_html_dom": "^4.8",  
19 - "ext-curl": "*",  
20 - "ext-zip": "*",  
21 - "ext-zlib": "*" 25 + "voku/simple_html_dom": "^4.8"
22 }, 26 },
23 "require-dev": { 27 "require-dev": {
24 "facade/ignition": "^2.5", 28 "facade/ignition": "^2.5",
@@ -38,3 +38,5 @@ @@ -38,3 +38,5 @@
38 Route::get('/update_robots', [NoticeController::class, 'updateRobots']); 38 Route::get('/update_robots', [NoticeController::class, 'updateRobots']);
39 }); 39 });
40 40
  41 +Route::any('/qr_pdf',[\App\Http\Controllers\Api\ToolController::class,'addQrToPdf']);
  42 +