본문 바로가기
  • 컴공생의 공부 일기
  • 공부보단 일기에 가까운 것 같은
  • 블로그
짜잘IT

☄️FPDF 라이브러리 add_font() 메서드 경로 인자 트러블슈팅

by 정람지 2025. 2. 13.

FPDF

from fpdf import FPDF
from PIL import Image
import os

def create_pdf(result_json):

    # ✅ PDF 생성
    pdf = FPDF()
    pdf.set_auto_page_break(auto=True, margin=15)
    pdf.add_page()

    # ✅ 한글 폰트 추가
    FONT_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "assets", "font")
    pdf.add_font("Nanum", "", os.path.join(FONT_FOLDER, "NanumGothic.ttf"), uni=True)
    pdf.add_font("Nanum", "B", os.path.join(FONT_FOLDER, "NanumGothicBold.ttf"), uni=True)

 

 

지속적 오류

 

 

pdf.add_font("Nanum", "", './ai/resultMakeAgent/font/assets/NanumGothic.ttf', uni=True)

설정 시

RuntimeError: TTF Font file not found: ./ai/resultMakeAgent/font/assets/NanumGothic.ttf

 

 

FONT_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "assets", "font")
pdf.add_font("Nanum", "", os.path.join(FONT_FOLDER, "NanumGothic.ttf"), uni=True)

조니엘이 쳐준 절대 경로 사용 시

FileNotFoundError: [Errno 2] No such file or directory: './ai/resultMakeAgent/font/NanumGothic.ttf'

 

 

./font 위치로 폰트 파일 옮기고,

FONT_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "assets", "font")
pdf.add_font("Nanum", "", os.path.join(FONT_FOLDER, "NanumGothic.ttf"), uni=True)

절대 경로 사용 시

RuntimeError: TTF Font file not found: /home/viuron/Documents/newscraper/ai/resultMakeAgent/utils/../assets/font/NanumGothic.ttf

 

 

 

./font 위치에도 폰트 파일, 기존 ./ai/resultMakeAgent/font 경로에도 폰트 파일 추가,

FONT_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "assets", "font")
pdf.add_font("Nanum", "", os.path.join(FONT_FOLDER, "NanumGothic.ttf"), uni=True)

절대 경로 사용 시

성공

 

 

./font 위치에 폰트 파일,

pdf.add_font("Nanum", "", './ai/resultMakeAgent/font/NanumGothic.ttf', uni=True)

루트 상대 경로 사용 시

성공

 

 

 

 

아마 루트 디렉토리에 같이 있어야 되는 것으로 추정.

그렇다면 왜 add_font() 메서드에 경로 인자를 넣었는지?

Traceback (most recent call last):
  File "/home/viuron/Documents/newscraper/ai/resultMakeAgent/main.py", line 61, in <module>
    pdf_path = handle_pdf_generation(final_json)
  File "/home/viuron/Documents/newscraper/ai/resultMakeAgent/agents/groupchat_1_4.py", line 83, in handle_pdf_generation
    pdf_path = create_pdf(final_json)
  File "/home/viuron/Documents/newscraper/ai/resultMakeAgent/utils/pdf_generator.py", line 96, in create_pdf
    pdf.output(output_path)
  File "/home/viuron/Documents/newscraper/venv/lib/python3.10/site-packages/fpdf/fpdf.py", line 1065, in output
    self.close()
  File "/home/viuron/Documents/newscraper/venv/lib/python3.10/site-packages/fpdf/fpdf.py", line 246, in close
    self._enddoc()
  File "/home/viuron/Documents/newscraper/venv/lib/python3.10/site-packages/fpdf/fpdf.py", line 1637, in _enddoc
    self._putresources()
  File "/home/viuron/Documents/newscraper/venv/lib/python3.10/site-packages/fpdf/fpdf.py", line 1584, in _putresources
    self._putfonts()
  File "/home/viuron/Documents/newscraper/venv/lib/python3.10/site-packages/fpdf/fpdf.py", line 1288, in _putfonts
    ttfontstream = ttf.makeSubset(font['ttffile'], subset)
  File "/home/viuron/Documents/newscraper/venv/lib/python3.10/site-packages/fpdf/ttfonts.py", line 459, in makeSubset
    self.fh = open(file ,'rb')
FileNotFoundError: [Errno 2] No such file or directory: './ai/resultMakeAgent/font/NanumGothic.ttf'
Traceback (most recent call last): File "/home/viuron/Documents/newscraper/ai/resultMakeAgent/main.py", line 61, in <module> pdf_path = handle_pdf_generation(final_json) File "/home/viuron/Documents/newscraper/ai/resultMakeAgent/agents/groupchat_1_4.py", line 83, in handle_pdf_generation pdf_path = create_pdf(final_json) File "/home/viuron/Documents/newscraper/ai/resultMakeAgent/utils/pdf_generator.py", line 14, in create_pdf pdf.add_font("Nanum", "", './ai/resultMakeAgent/font/assets/NanumGothic.ttf', uni=True) File "/home/viuron/Documents/newscraper/venv/lib/python3.10/site-packages/fpdf/fpdf.py", line 469, in add_font raise RuntimeError("TTF Font file not found: %s" % fname) RuntimeError: TTF Font file not found: ./ai/resultMakeAgent/font/assets/NanumGothic.ttf

http://www.fpdf.org/en/doc/index.php

 

Documentation

 

www.fpdf.org

http://www.fpdf.org/

 

FPDF

 

www.fpdf.org

 

 

https://github.com/Setasign/FPDF

 

GitHub - Setasign/FPDF: FPDF is a PHP class which allows to generate PDF files with pure PHP. F from FPDF stands for Free: you m

FPDF is a PHP class which allows to generate PDF files with pure PHP. F from FPDF stands for Free: you may use it for any kind of usage and modify it to suit your needs. - Setasign/FPDF

github.com

뭐야 -- 기여 불가 아님?

내부 코드 좀 보고 이슈라도 날려 볼까 했는데

팍노잼..

 

도망 총총


+
✅ os.path.join()

  1. 운영체제(OS) 간 차이를 자동 처리
  2. 현재 파일(__file__)을 기준으로 경로를 찾을 수 있음
  3. 가독성 & 유지보수 용이
  4. 파일이 존재하는지 체크하기 쉬움
    • os.path.exists(FONT_FOLDER) 등을 활용하여 폰트 파일이 있는지 확인 가능

 

# 현재 실행 중인 파일의 절대 경로 출력

print(os.path.abspath(__file__))

 

# 현재 파일이 위치한 디렉토리 출력

print(os.path.dirname(os.path.abspath(__file__)))