|
...
|
...
|
@@ -37,6 +37,37 @@ protected function error($message = 'error', $status = 400, $data = []) |
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 生成二维码
|
|
|
|
* @param Request $request
|
|
|
|
* @return string
|
|
|
|
* @author Akun
|
|
|
|
* @date 2026/03/05 10:28
|
|
|
|
*/
|
|
|
|
public function createQr(Request $request)
|
|
|
|
{
|
|
|
|
$qrText = $request->input('qr_text');
|
|
|
|
if (empty($qrText)) {
|
|
|
|
return $this->error('二维码内容未知');
|
|
|
|
}
|
|
|
|
|
|
|
|
// 生成二维码图片
|
|
|
|
try {
|
|
|
|
$qrImage = 'www/wwwroot/qrcode/qrcode_' . time() . '.png';
|
|
|
|
$renderer = new ImageRenderer(
|
|
|
|
new RendererStyle(300, 0),
|
|
|
|
new GdImageBackEnd() // 使用 GD 后端
|
|
|
|
);
|
|
|
|
$writer = new Writer($renderer);
|
|
|
|
|
|
|
|
$writer->writeFile($qrText, $qrImage);
|
|
|
|
|
|
|
|
return $this->success(['qr_image' => $qrImage]);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return $this->error($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pdf文件添加二维码
|
|
|
|
* @param Request $request
|
|
|
|
* @return string
|
|
...
|
...
|
@@ -52,73 +83,66 @@ public function addQrToPdf(Request $request) |
|
|
|
{
|
|
|
|
$sourceFile = $request->input('original_pdf');
|
|
|
|
if (empty($sourceFile)) {
|
|
|
|
return $this->error('原始pdf地址未知');
|
|
|
|
return $this->error('pdf地址未知');
|
|
|
|
}
|
|
|
|
if (!file_exists($sourceFile)) {
|
|
|
|
return $this->error('原始pdf文件不存在');
|
|
|
|
return $this->error('pdf文件不存在');
|
|
|
|
}
|
|
|
|
$qrText = $request->input('qr_text');
|
|
|
|
if (empty($qrText)) {
|
|
|
|
return $this->error('二维码内容未知');
|
|
|
|
$qrImage = $request->input('qr_image');
|
|
|
|
if (empty($qrImage)) {
|
|
|
|
return $this->error('二维码地址未知');
|
|
|
|
}
|
|
|
|
if (!file_exists($qrImage)) {
|
|
|
|
return $this->error('二维码文件不存在');
|
|
|
|
}
|
|
|
|
|
|
|
|
// 生成二维码图片
|
|
|
|
$qrImage = public_path('qrcode_temp.png');
|
|
|
|
$renderer = new ImageRenderer(
|
|
|
|
new RendererStyle(300, 0),
|
|
|
|
new GdImageBackEnd() // 使用 GD 后端
|
|
|
|
);
|
|
|
|
$writer = new Writer($renderer);
|
|
|
|
|
|
|
|
// 生成 PNG 图片数据
|
|
|
|
$writer->writeFile($qrText, $qrImage);
|
|
|
|
|
|
|
|
$pdf = new Fpdi();
|
|
|
|
try {
|
|
|
|
$pdf = new Fpdi();
|
|
|
|
|
|
|
|
$pageCount = $pdf->setSourceFile($sourceFile);
|
|
|
|
$pageCount = $pdf->setSourceFile($sourceFile);
|
|
|
|
|
|
|
|
for ($i = 1; $i <= $pageCount; $i++) {
|
|
|
|
for ($i = 1; $i <= $pageCount; $i++) {
|
|
|
|
|
|
|
|
$tpl = $pdf->importPage($i);
|
|
|
|
$size = $pdf->getTemplateSize($tpl);
|
|
|
|
$tpl = $pdf->importPage($i);
|
|
|
|
$size = $pdf->getTemplateSize($tpl);
|
|
|
|
|
|
|
|
$pdf->AddPage($size['orientation'], [$size['width'], $size['height']]);
|
|
|
|
$pdf->useTemplate($tpl);
|
|
|
|
$pdf->AddPage($size['orientation'], [$size['width'], $size['height']]);
|
|
|
|
$pdf->useTemplate($tpl);
|
|
|
|
|
|
|
|
// ✅ 只在第一页添加二维码
|
|
|
|
if ($i === 1) {
|
|
|
|
// ✅ 只在第一页添加二维码
|
|
|
|
if ($i === 1) {
|
|
|
|
|
|
|
|
$qrWidth = 30; // 二维码宽度(单位:mm)
|
|
|
|
$margin = 5; // 距离边缘间距(mm)
|
|
|
|
$qrWidth = 30; // 二维码宽度(单位:mm)
|
|
|
|
$margin = 5; // 距离边缘间距(mm)
|
|
|
|
|
|
|
|
$x = $size['width'] - $qrWidth - $margin;
|
|
|
|
$y = $margin;
|
|
|
|
$x = $size['width'] - $qrWidth - $margin;
|
|
|
|
$y = $margin;
|
|
|
|
|
|
|
|
$pdf->Image($qrImage, $x, $y, $qrWidth);
|
|
|
|
$pdf->Image($qrImage, $x, $y, $qrWidth);
|
|
|
|
|
|
|
|
$text = 'Scan to verify the original MTC on official website';
|
|
|
|
$text = 'Scan to verify the original MTC on official website';
|
|
|
|
|
|
|
|
// 设置字体(必须设置,否则不显示)
|
|
|
|
$pdf->SetFont('Arial', '', 8);
|
|
|
|
// 设置字体(必须设置,否则不显示)
|
|
|
|
$pdf->SetFont('Arial', '', 8);
|
|
|
|
|
|
|
|
// 计算文字位置
|
|
|
|
$textY = $y + $qrWidth + 2; // 二维码下方2mm
|
|
|
|
// 计算文字位置
|
|
|
|
$textY = $y + $qrWidth + 2; // 二维码下方2mm
|
|
|
|
|
|
|
|
// 设置光标位置
|
|
|
|
$pdf->SetXY($x - 1, $textY);
|
|
|
|
// 设置光标位置
|
|
|
|
$pdf->SetXY($x - 1, $textY);
|
|
|
|
|
|
|
|
// 固定宽度,自动换行
|
|
|
|
$pdf->MultiCell($qrWidth + 10, 4, $text, 0, 'L');
|
|
|
|
// 固定宽度,自动换行
|
|
|
|
$pdf->MultiCell($qrWidth + 10, 4, $text, 0, 'L');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 生成新的pdf文件
|
|
|
|
$outputFile = substr($sourceFile, 0, -4) . '_' . time() . '.pdf';
|
|
|
|
$pdf->Output('F', $outputFile);
|
|
|
|
|
|
|
|
// 删除临时二维码图片
|
|
|
|
unlink($qrImage);
|
|
|
|
// 生成新的pdf文件
|
|
|
|
$outputFile = substr($sourceFile, 0, -4) . '_' . time() . '.pdf';
|
|
|
|
$pdf->Output('F', $outputFile);
|
|
|
|
|
|
|
|
return $this->success(['pdf_file' => $outputFile]);
|
|
|
|
return $this->success(['target_pdf' => $outputFile]);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return $this->error($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|