CaiWuXiTong/app/Support/CourierQuotePdf.php

42 lines
1.2 KiB
PHP

<?php
namespace App\Support;
use App\Models\Business\CourierQuoteVersion;
use RuntimeException;
final class CourierQuotePdf
{
public function __construct(private readonly CourierQuoteConfirmation $confirmation)
{
}
public function render(CourierQuoteVersion $version): string
{
$data = $this->confirmation->data($version);
$html = view('business.pdf.courier-quote', [
'version' => $version,
'change' => $version->change,
'values' => $data['values'],
'formulas' => $data['formulas'],
'partyA' => $data['party_a'],
'expiryDate' => $data['expiry_date'],
'faceFee' => $data['face_fee'],
'bubbleCoefficient' => $data['bubble_coefficient'],
'generatedAt' => now(),
'fontPath' => str_replace('\\', '/', resource_path('fonts/DroidSansFallback.ttf')),
])->render();
$pdf = app('dompdf.wrapper');
$pdf->setPaper('a4', 'landscape');
$pdf->loadHTML($html, 'UTF-8');
$bytes = $pdf->output();
if (! is_string($bytes) || ! str_starts_with($bytes, '%PDF-')) {
throw new RuntimeException('报价 PDF 生成失败。');
}
return $bytes;
}
}