CaiWuXiTong/app/Support/CourierAgreementPdf.php
limeng a46d4dc678 修复签署文件日期未生成 (#225)
统一使用 signature_date 变量,并在推送 E 签宝前写入协议与报价确认单。
2026-09-21 10:02:37 +08:00

53 lines
1.8 KiB
PHP

<?php
namespace App\Support;
use App\Models\Business\CourierAgreementSignature;
use Illuminate\Support\Carbon;
use RuntimeException;
final class CourierAgreementPdf
{
public function render(CourierAgreementSignature $signature): string
{
$payload = $signature->payload ?? [];
$signatureDate = (string) ($payload['signature_date'] ?? $payload['entity_proof_date'] ?? $payload['shared_proof_date'] ?? '');
try {
$signatureDate = $signatureDate !== '' ? Carbon::parse($signatureDate)->format('Y年n月j日') : '待填写';
} catch (\Throwable) {
$signatureDate = '待填写';
}
$html = view('business.pdf.courier-agreement', [
'signature' => $signature,
'type' => $signature->document_type,
'title' => $this->title($signature),
'payload' => $payload,
'signatureDate' => $signatureDate,
'fontPath' => str_replace('\\', '/', resource_path('fonts/DroidSansFallback.ttf')),
])->render();
$pdf = app('dompdf.wrapper');
$pdf->setPaper('a4', 'portrait');
$pdf->loadHTML($html, 'UTF-8');
$bytes = $pdf->output();
if (! is_string($bytes) || ! str_starts_with($bytes, '%PDF-')) {
throw new RuntimeException('协议 PDF 生成失败。');
}
return $bytes;
}
private function title(CourierAgreementSignature $signature): string
{
return match ($signature->document_type) {
'account-proof' => '收款帐户资料证明',
'account-change' => '收款帐户资料变更证明',
'entity-change' => '公司主体变更证明',
'shared-contract' => '证明',
'cooperation-contract' => '服务协议',
default => '授权委托书',
};
}
}