From 084e0f1ff6f12fa552319bda21eeeb24e2aafead Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=B3=B4=EA=B3=A4?= Date: Wed, 11 Mar 2026 14:05:51 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20[esign]=20PDF=20=ED=95=9C=EA=B8=80=20?= =?UTF-8?q?=ED=8F=B0=ED=8A=B8=20=EA=B9=A8=EC=A7=90=20=EC=88=98=EC=A0=95=20?= =?UTF-8?q?=E2=80=94=20K=5FPATH=5FFONTS=20=EC=9D=98=EC=A1=B4=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - K_PATH_FONTS 상수가 TCPDF vendor 경로로 선점되면 pretendard 폰트를 못 찾는 문제 - AddFont()으로 storage/fonts/tcpdf/ 경로를 직접 지정하여 확실하게 로드 - 폴백: resources/fonts/Pretendard-Regular.ttf → helvetica --- app/Services/ESign/PdfSignatureService.php | 53 ++++++++++------------ 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/app/Services/ESign/PdfSignatureService.php b/app/Services/ESign/PdfSignatureService.php index 2b0d5c52..ff0de076 100644 --- a/app/Services/ESign/PdfSignatureService.php +++ b/app/Services/ESign/PdfSignatureService.php @@ -10,48 +10,45 @@ class PdfSignatureService { - private ?string $koreanFontName = null; - - public function __construct() - { - // TCPDF 클래스 로드 전에 K_PATH_FONTS를 쓰기 가능한 디렉토리로 설정 - // (운영서버에서 vendor/tecnickcom/tcpdf/fonts/ 쓰기 권한 없는 문제 방지) - if (! defined('K_PATH_FONTS')) { - $tcpdfFontsDir = dirname(__DIR__, 3).'/storage/fonts/tcpdf/'; - if (is_dir($tcpdfFontsDir)) { - define('K_PATH_FONTS', $tcpdfFontsDir); - } - } - } + private bool $fontRegistered = false; /** - * Pretendard 한글 폰트를 등록하고 폰트 이름을 반환한다. - * 사전 생성된 폰트 정의 파일이 storage/fonts/tcpdf/에 있으면 바로 사용한다. + * Pretendard 한글 폰트를 PDF 인스턴스에 직접 등록한다. + * K_PATH_FONTS 상수에 의존하지 않고 명시적 경로로 폰트를 로드한다. */ - private function getKoreanFont(): string + private function ensureKoreanFont(Fpdi $pdf): string { - if ($this->koreanFontName) { - return $this->koreanFontName; + if ($this->fontRegistered) { + return 'pretendard'; } - // 사전 생성된 폰트 정의 파일이 K_PATH_FONTS에 있으면 바로 사용 - if (defined('K_PATH_FONTS') && file_exists(K_PATH_FONTS.'pretendard.php')) { - $this->koreanFontName = 'pretendard'; + $fontDir = dirname(__DIR__, 3).'/storage/fonts/tcpdf/'; + $fontFile = $fontDir.'pretendard.php'; - return $this->koreanFontName; + // 1순위: 사전 빌드된 폰트 정의 파일로 직접 등록 (K_PATH_FONTS 무관) + if (file_exists($fontFile)) { + $pdf->AddFont('pretendard', '', $fontFile); + $this->fontRegistered = true; + + return 'pretendard'; } - // 폴백: TTF에서 런타임 생성 시도 - $fontPath = storage_path('fonts/Pretendard-Regular.ttf'); - if (file_exists($fontPath)) { + // 2순위: TTF에서 런타임 생성 + $ttfPath = resource_path('fonts/Pretendard-Regular.ttf'); + if (file_exists($ttfPath)) { try { - $this->koreanFontName = \TCPDF_FONTS::addTTFfont($fontPath, 'TrueTypeUnicode', '', 96); + $fontName = \TCPDF_FONTS::addTTFfont($ttfPath, 'TrueTypeUnicode', '', 96); + if ($fontName) { + $this->fontRegistered = true; + + return $fontName; + } } catch (\Throwable $e) { Log::warning('TCPDF 한글 폰트 등록 실패', ['error' => $e->getMessage()]); } } - return $this->koreanFontName ?: 'helvetica'; + return 'helvetica'; } /** @@ -320,7 +317,7 @@ private function renderText(Fpdi $pdf, string $text, float $x, float $y, float $ } } - $pdf->SetFont($this->getKoreanFont(), '', $fontSize); + $pdf->SetFont($this->ensureKoreanFont($pdf), '', $fontSize); $pdf->SetTextColor(0, 0, 0); // 텍스트를 지정된 정렬 방식으로 배치 (L=왼쪽, C=가운데, R=오른쪽)