diff --git a/app/Services/ESign/PdfSignatureService.php b/app/Services/ESign/PdfSignatureService.php index b170ea01..cfa14ddb 100644 --- a/app/Services/ESign/PdfSignatureService.php +++ b/app/Services/ESign/PdfSignatureService.php @@ -13,27 +13,38 @@ class PdfSignatureService private ?string $koreanFontName = null; /** - * Pretendard 한글 폰트를 등록하고 폰트 이름을 반환한다. - * PDF 문서와 동일한 폰트로 필드 텍스트를 렌더링하여 일관성을 유지한다. + * PDF 인스턴스에 Pretendard 한글 폰트를 등록한다. + * 폰트 캐시를 storage에 생성하고, PDF 인스턴스에 전체 경로로 등록하여 + * vendor 디렉토리 쓰기 권한 문제를 방지한다. */ - private function getKoreanFont(): string + private function registerKoreanFont(Fpdi $pdf): void { - if ($this->koreanFontName) { - return $this->koreanFontName; + $fontPath = storage_path('fonts/Pretendard-Regular.ttf'); + if (! file_exists($fontPath)) { + return; } - // 1순위: 프로젝트 내장 Pretendard (로컬/서버 공통) - $fontPath = storage_path('fonts/Pretendard-Regular.ttf'); + $fontCacheDir = storage_path('app/private/fonts'); + $fontDefFile = $fontCacheDir.'/pretendard.php'; - if (file_exists($fontPath)) { - // 폰트 캐시를 storage에 저장 (vendor 디렉토리 쓰기 권한 문제 방지) - $fontCacheDir = storage_path('app/private/fonts'); + // 폰트 캐시 파일이 없으면 생성 + if (! file_exists($fontDefFile)) { if (! is_dir($fontCacheDir)) { mkdir($fontCacheDir, 0775, true); } - $this->koreanFontName = \TCPDF_FONTS::addTTFfont($fontPath, 'TrueTypeUnicode', '', 96, $fontCacheDir.'/'); + \TCPDF_FONTS::addTTFfont($fontPath, 'TrueTypeUnicode', '', 96, $fontCacheDir.'/'); } + // PDF 인스턴스에 전체 경로로 폰트 등록 (K_PATH_FONTS 의존 제거) + $pdf->AddFont('pretendard', '', $fontDefFile); + $this->koreanFontName = 'pretendard'; + } + + /** + * 등록된 한글 폰트 이름을 반환한다. + */ + private function getKoreanFont(): string + { return $this->koreanFontName ?: 'helvetica'; } @@ -53,6 +64,7 @@ public function mergeSignatures(EsignContract $contract): string $pdf = new Fpdi; $pdf->setPrintHeader(false); $pdf->setPrintFooter(false); + $this->registerKoreanFont($pdf); $pageCount = $pdf->setSourceFile($originalPath); @@ -123,6 +135,7 @@ public function generatePreview(EsignContract $contract): string $pdf = new Fpdi; $pdf->setPrintHeader(false); $pdf->setPrintFooter(false); + $this->registerKoreanFont($pdf); $pageCount = $pdf->setSourceFile($originalPath);