CaiWuXiTong/app/Support/CourierAgreementPdf.php

44 lines
1.4 KiB
PHP

<?php
namespace App\Support;
use App\Models\Business\CourierAgreementSignature;
use RuntimeException;
final class CourierAgreementPdf
{
public function render(CourierAgreementSignature $signature): string
{
$html = view('business.pdf.courier-agreement', [
'signature' => $signature,
'type' => $signature->document_type,
'title' => $this->title($signature),
'payload' => $signature->payload ?? [],
'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 => '授权委托书',
};
}
}