|
@@ -37,6 +37,37 @@ protected function error($message = 'error', $status = 400, $data = []) |
|
@@ -37,6 +37,37 @@ protected function error($message = 'error', $status = 400, $data = []) |
|
37
|
}
|
37
|
}
|
|
38
|
|
38
|
|
|
39
|
/**
|
39
|
/**
|
|
|
|
40
|
+ * 生成二维码
|
|
|
|
41
|
+ * @param Request $request
|
|
|
|
42
|
+ * @return string
|
|
|
|
43
|
+ * @author Akun
|
|
|
|
44
|
+ * @date 2026/03/05 10:28
|
|
|
|
45
|
+ */
|
|
|
|
46
|
+ public function createQr(Request $request)
|
|
|
|
47
|
+ {
|
|
|
|
48
|
+ $qrText = $request->input('qr_text');
|
|
|
|
49
|
+ if (empty($qrText)) {
|
|
|
|
50
|
+ return $this->error('二维码内容未知');
|
|
|
|
51
|
+ }
|
|
|
|
52
|
+
|
|
|
|
53
|
+ // 生成二维码图片
|
|
|
|
54
|
+ try {
|
|
|
|
55
|
+ $qrImage = 'www/wwwroot/qrcode/qrcode_' . time() . '.png';
|
|
|
|
56
|
+ $renderer = new ImageRenderer(
|
|
|
|
57
|
+ new RendererStyle(300, 0),
|
|
|
|
58
|
+ new GdImageBackEnd() // 使用 GD 后端
|
|
|
|
59
|
+ );
|
|
|
|
60
|
+ $writer = new Writer($renderer);
|
|
|
|
61
|
+
|
|
|
|
62
|
+ $writer->writeFile($qrText, $qrImage);
|
|
|
|
63
|
+
|
|
|
|
64
|
+ return $this->success(['qr_image' => $qrImage]);
|
|
|
|
65
|
+ } catch (\Exception $e) {
|
|
|
|
66
|
+ return $this->error($e->getMessage());
|
|
|
|
67
|
+ }
|
|
|
|
68
|
+ }
|
|
|
|
69
|
+
|
|
|
|
70
|
+ /**
|
|
40
|
* pdf文件添加二维码
|
71
|
* pdf文件添加二维码
|
|
41
|
* @param Request $request
|
72
|
* @param Request $request
|
|
42
|
* @return string
|
73
|
* @return string
|
|
@@ -52,73 +83,66 @@ public function addQrToPdf(Request $request) |
|
@@ -52,73 +83,66 @@ public function addQrToPdf(Request $request) |
|
52
|
{
|
83
|
{
|
|
53
|
$sourceFile = $request->input('original_pdf');
|
84
|
$sourceFile = $request->input('original_pdf');
|
|
54
|
if (empty($sourceFile)) {
|
85
|
if (empty($sourceFile)) {
|
|
55
|
- return $this->error('原始pdf地址未知');
|
86
|
+ return $this->error('pdf地址未知');
|
|
56
|
}
|
87
|
}
|
|
57
|
if (!file_exists($sourceFile)) {
|
88
|
if (!file_exists($sourceFile)) {
|
|
58
|
- return $this->error('原始pdf文件不存在');
|
89
|
+ return $this->error('pdf文件不存在');
|
|
59
|
}
|
90
|
}
|
|
60
|
- $qrText = $request->input('qr_text');
|
|
|
|
61
|
- if (empty($qrText)) {
|
|
|
|
62
|
- return $this->error('二维码内容未知');
|
91
|
+ $qrImage = $request->input('qr_image');
|
|
|
|
92
|
+ if (empty($qrImage)) {
|
|
|
|
93
|
+ return $this->error('二维码地址未知');
|
|
|
|
94
|
+ }
|
|
|
|
95
|
+ if (!file_exists($qrImage)) {
|
|
|
|
96
|
+ return $this->error('二维码文件不存在');
|
|
63
|
}
|
97
|
}
|
|
64
|
|
98
|
|
|
65
|
- // 生成二维码图片
|
|
|
|
66
|
- $qrImage = public_path('qrcode_temp.png');
|
|
|
|
67
|
- $renderer = new ImageRenderer(
|
|
|
|
68
|
- new RendererStyle(300, 0),
|
|
|
|
69
|
- new GdImageBackEnd() // 使用 GD 后端
|
|
|
|
70
|
- );
|
|
|
|
71
|
- $writer = new Writer($renderer);
|
|
|
|
72
|
-
|
|
|
|
73
|
- // 生成 PNG 图片数据
|
|
|
|
74
|
- $writer->writeFile($qrText, $qrImage);
|
|
|
|
75
|
-
|
|
|
|
76
|
- $pdf = new Fpdi();
|
99
|
+ try {
|
|
|
|
100
|
+ $pdf = new Fpdi();
|
|
77
|
|
101
|
|
|
78
|
- $pageCount = $pdf->setSourceFile($sourceFile);
|
102
|
+ $pageCount = $pdf->setSourceFile($sourceFile);
|
|
79
|
|
103
|
|
|
80
|
- for ($i = 1; $i <= $pageCount; $i++) {
|
104
|
+ for ($i = 1; $i <= $pageCount; $i++) {
|
|
81
|
|
105
|
|
|
82
|
- $tpl = $pdf->importPage($i);
|
|
|
|
83
|
- $size = $pdf->getTemplateSize($tpl);
|
106
|
+ $tpl = $pdf->importPage($i);
|
|
|
|
107
|
+ $size = $pdf->getTemplateSize($tpl);
|
|
84
|
|
108
|
|
|
85
|
- $pdf->AddPage($size['orientation'], [$size['width'], $size['height']]);
|
|
|
|
86
|
- $pdf->useTemplate($tpl);
|
109
|
+ $pdf->AddPage($size['orientation'], [$size['width'], $size['height']]);
|
|
|
|
110
|
+ $pdf->useTemplate($tpl);
|
|
87
|
|
111
|
|
|
88
|
- // ✅ 只在第一页添加二维码
|
|
|
|
89
|
- if ($i === 1) {
|
112
|
+ // ✅ 只在第一页添加二维码
|
|
|
|
113
|
+ if ($i === 1) {
|
|
90
|
|
114
|
|
|
91
|
- $qrWidth = 30; // 二维码宽度(单位:mm)
|
|
|
|
92
|
- $margin = 5; // 距离边缘间距(mm)
|
115
|
+ $qrWidth = 30; // 二维码宽度(单位:mm)
|
|
|
|
116
|
+ $margin = 5; // 距离边缘间距(mm)
|
|
93
|
|
117
|
|
|
94
|
- $x = $size['width'] - $qrWidth - $margin;
|
|
|
|
95
|
- $y = $margin;
|
118
|
+ $x = $size['width'] - $qrWidth - $margin;
|
|
|
|
119
|
+ $y = $margin;
|
|
96
|
|
120
|
|
|
97
|
- $pdf->Image($qrImage, $x, $y, $qrWidth);
|
121
|
+ $pdf->Image($qrImage, $x, $y, $qrWidth);
|
|
98
|
|
122
|
|
|
99
|
- $text = 'Scan to verify the original MTC on official website';
|
123
|
+ $text = 'Scan to verify the original MTC on official website';
|
|
100
|
|
124
|
|
|
101
|
- // 设置字体(必须设置,否则不显示)
|
|
|
|
102
|
- $pdf->SetFont('Arial', '', 8);
|
125
|
+ // 设置字体(必须设置,否则不显示)
|
|
|
|
126
|
+ $pdf->SetFont('Arial', '', 8);
|
|
103
|
|
127
|
|
|
104
|
- // 计算文字位置
|
|
|
|
105
|
- $textY = $y + $qrWidth + 2; // 二维码下方2mm
|
128
|
+ // 计算文字位置
|
|
|
|
129
|
+ $textY = $y + $qrWidth + 2; // 二维码下方2mm
|
|
106
|
|
130
|
|
|
107
|
- // 设置光标位置
|
|
|
|
108
|
- $pdf->SetXY($x - 1, $textY);
|
131
|
+ // 设置光标位置
|
|
|
|
132
|
+ $pdf->SetXY($x - 1, $textY);
|
|
109
|
|
133
|
|
|
110
|
- // 固定宽度,自动换行
|
|
|
|
111
|
- $pdf->MultiCell($qrWidth + 10, 4, $text, 0, 'L');
|
134
|
+ // 固定宽度,自动换行
|
|
|
|
135
|
+ $pdf->MultiCell($qrWidth + 10, 4, $text, 0, 'L');
|
|
|
|
136
|
+ }
|
|
112
|
}
|
137
|
}
|
|
113
|
- }
|
|
|
|
114
|
-
|
|
|
|
115
|
- // 生成新的pdf文件
|
|
|
|
116
|
- $outputFile = substr($sourceFile, 0, -4) . '_' . time() . '.pdf';
|
|
|
|
117
|
- $pdf->Output('F', $outputFile);
|
|
|
|
118
|
|
138
|
|
|
119
|
- // 删除临时二维码图片
|
|
|
|
120
|
- unlink($qrImage);
|
139
|
+ // 生成新的pdf文件
|
|
|
|
140
|
+ $outputFile = substr($sourceFile, 0, -4) . '_' . time() . '.pdf';
|
|
|
|
141
|
+ $pdf->Output('F', $outputFile);
|
|
121
|
|
142
|
|
|
122
|
- return $this->success(['pdf_file' => $outputFile]);
|
143
|
+ return $this->success(['target_pdf' => $outputFile]);
|
|
|
|
144
|
+ } catch (\Exception $e) {
|
|
|
|
145
|
+ return $this->error($e->getMessage());
|
|
|
|
146
|
+ }
|
|
123
|
}
|
147
|
}
|
|
124
|
} |
148
|
} |