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
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()
- 운영체제(OS) 간 차이를 자동 처리
- 현재 파일(__file__)을 기준으로 경로를 찾을 수 있음
- 가독성 & 유지보수 용이
- 파일이 존재하는지 체크하기 쉬움
- os.path.exists(FONT_FOLDER) 등을 활용하여 폰트 파일이 있는지 확인 가능
# 현재 실행 중인 파일의 절대 경로 출력
print(os.path.abspath(__file__))
# 현재 파일이 위치한 디렉토리 출력
print(os.path.dirname(os.path.abspath(__file__)))
'짜잘IT' 카테고리의 다른 글
프로메테우스 지원서 면접준비 (0) | 2025.02.16 |
---|---|
이화여대 Euron 8기 모집 지원서 아카이브 (0) | 2025.02.15 |
프로메테우스 지원서 아카이브 (0) | 2025.02.09 |
[카카오페이] 선행기술개발TF 업무 어시스턴트 지원서 - 6개월?포기!복학해야지,, (0) | 2024.12.16 |
[NAVER] Music Auto-tagging 연구 인턴 (체험형) 지원서 (0) | 2024.11.27 |