Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -971,67 +971,81 @@ async def convert_text_to_pdf(text_content: str, title: str) -> str:
|
|
| 971 |
# ์๊ตฌ ์ ์ฅ์์ ํ์ผ ๊ฒฝ๋ก
|
| 972 |
file_path = PERMANENT_PDF_DIR / filename
|
| 973 |
|
| 974 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 975 |
from reportlab.lib.pagesizes import letter
|
| 976 |
-
from reportlab.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 977 |
|
| 978 |
-
|
| 979 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 980 |
|
| 981 |
-
#
|
| 982 |
-
|
| 983 |
|
| 984 |
# ์ ๋ชฉ ์ถ๊ฐ
|
| 985 |
-
|
| 986 |
-
|
| 987 |
-
|
| 988 |
-
# ๋ณธ๋ฌธ ํ
์คํธ ์ถ๊ฐ
|
| 989 |
-
c.setFont("Helvetica", 11)
|
| 990 |
-
y_position = page_height - 100
|
| 991 |
-
line_height = 14
|
| 992 |
|
| 993 |
-
# ํ
์คํธ๋ฅผ ๋จ๋ฝ์ผ๋ก
|
| 994 |
paragraphs = text_content.split('\n\n')
|
| 995 |
-
|
| 996 |
for para in paragraphs:
|
| 997 |
-
if
|
| 998 |
-
|
| 999 |
-
|
| 1000 |
-
|
| 1001 |
-
|
| 1002 |
-
|
| 1003 |
-
|
| 1004 |
-
|
| 1005 |
-
|
| 1006 |
-
|
| 1007 |
-
import textwrap
|
| 1008 |
-
wrapped_lines = textwrap.wrap(line, width=max_chars_per_line)
|
| 1009 |
-
|
| 1010 |
-
for wrapped_line in wrapped_lines:
|
| 1011 |
-
# ํ์ด์ง ๋ฐ๊ฟ ํ์ธ
|
| 1012 |
-
if y_position < 50:
|
| 1013 |
-
c.showPage()
|
| 1014 |
-
c.setFont("Helvetica", 11)
|
| 1015 |
-
y_position = page_height - 50
|
| 1016 |
-
|
| 1017 |
-
try:
|
| 1018 |
-
# ASCII ๋ฌธ์๋ง ์ฒ๋ฆฌ
|
| 1019 |
-
ascii_line = ''.join(c if ord(c) < 128 else ' ' for c in wrapped_line)
|
| 1020 |
-
c.drawString(50, y_position, ascii_line)
|
| 1021 |
-
except:
|
| 1022 |
-
# ์ค๋ฅ ๋ฐ์ ์ ๊ณต๋ฐฑ์ผ๋ก ๋์ฒด
|
| 1023 |
-
c.drawString(50, y_position, "[ํ
์คํธ ๋ณํ ์ค๋ฅ]")
|
| 1024 |
-
|
| 1025 |
-
y_position -= line_height
|
| 1026 |
-
|
| 1027 |
-
# ๋จ๋ฝ ๊ฐ ๊ฐ๊ฒฉ
|
| 1028 |
-
y_position -= 10
|
| 1029 |
|
| 1030 |
-
#
|
| 1031 |
-
|
|
|
|
| 1032 |
|
| 1033 |
# ๋ฉ์ธ ๋๋ ํ ๋ฆฌ์๋ ๋ณต์ฌ
|
| 1034 |
-
|
|
|
|
| 1035 |
|
| 1036 |
# PDF ID ์์ฑ ๋ฐ ๋ฉํ๋ฐ์ดํฐ ์ ์ฅ
|
| 1037 |
pdf_id = generate_pdf_id(filename)
|
|
|
|
| 971 |
# ์๊ตฌ ์ ์ฅ์์ ํ์ผ ๊ฒฝ๋ก
|
| 972 |
file_path = PERMANENT_PDF_DIR / filename
|
| 973 |
|
| 974 |
+
# ํ๊ธ ํฐํธ ๋ฑ๋ก - ์
๋ก๋๋ MaruBuri-SemiBold.ttf ์ฌ์ฉ
|
| 975 |
+
from reportlab.pdfbase import pdfmetrics
|
| 976 |
+
from reportlab.pdfbase.ttfonts import TTFont
|
| 977 |
+
|
| 978 |
+
# ํฐํธ ๊ฒฝ๋ก ์ค์ (app.py์ ๊ฐ์ ๋๋ ํ ๋ฆฌ์ ์๋ ํฐํธ ์ฌ์ฉ)
|
| 979 |
+
font_path = BASE / "MaruBuri-SemiBold.ttf"
|
| 980 |
+
|
| 981 |
+
# ํฐํธ ๋ฑ๋ก
|
| 982 |
+
font_name = "MaruBuri"
|
| 983 |
+
if font_path.exists():
|
| 984 |
+
pdfmetrics.registerFont(TTFont(font_name, str(font_path)))
|
| 985 |
+
logger.info(f"ํ๊ธ ํฐํธ ๋ฑ๋ก ์ฑ๊ณต: {font_path}")
|
| 986 |
+
else:
|
| 987 |
+
font_name = "Helvetica"
|
| 988 |
+
logger.warning(f"ํ๊ธ ํฐํธ ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค: {font_path}. ๊ธฐ๋ณธ ํฐํธ๋ฅผ ์ฌ์ฉํฉ๋๋ค.")
|
| 989 |
+
|
| 990 |
+
# ์์ PDF ํ์ผ ์์ฑ
|
| 991 |
+
pdf_buffer = io.BytesIO()
|
| 992 |
+
|
| 993 |
+
# ํ๊ธ ์ง์์ ์ํ ์คํ์ผ ์ค์
|
| 994 |
from reportlab.lib.pagesizes import letter
|
| 995 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
| 996 |
+
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
| 997 |
+
from reportlab.lib.enums import TA_CENTER, TA_LEFT
|
| 998 |
+
|
| 999 |
+
doc = SimpleDocTemplate(pdf_buffer, pagesize=letter, encoding='utf-8')
|
| 1000 |
+
|
| 1001 |
+
# ์ฌ์ฉ์ ์ ์ ์คํ์ผ ์์ฑ
|
| 1002 |
+
title_style = ParagraphStyle(
|
| 1003 |
+
name='CustomTitle',
|
| 1004 |
+
fontName=font_name,
|
| 1005 |
+
fontSize=18,
|
| 1006 |
+
leading=22,
|
| 1007 |
+
alignment=TA_CENTER,
|
| 1008 |
+
spaceAfter=20
|
| 1009 |
+
)
|
| 1010 |
|
| 1011 |
+
normal_style = ParagraphStyle(
|
| 1012 |
+
name='CustomNormal',
|
| 1013 |
+
fontName=font_name,
|
| 1014 |
+
fontSize=12,
|
| 1015 |
+
leading=15,
|
| 1016 |
+
alignment=TA_LEFT,
|
| 1017 |
+
spaceBefore=6,
|
| 1018 |
+
spaceAfter=6
|
| 1019 |
+
)
|
| 1020 |
|
| 1021 |
+
# ๋ด์ฉ์ ๋ฌธ๋จ์ผ๋ก ๋ถํ
|
| 1022 |
+
content = []
|
| 1023 |
|
| 1024 |
# ์ ๋ชฉ ์ถ๊ฐ
|
| 1025 |
+
content.append(Paragraph(title, title_style))
|
| 1026 |
+
content.append(Spacer(1, 20))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1027 |
|
| 1028 |
+
# ํ
์คํธ๋ฅผ ๋จ๋ฝ์ผ๋ก ๋ถ๋ฆฌํ์ฌ ์ถ๊ฐ
|
| 1029 |
paragraphs = text_content.split('\n\n')
|
|
|
|
| 1030 |
for para in paragraphs:
|
| 1031 |
+
if para.strip():
|
| 1032 |
+
# XML ํน์๋ฌธ์ ์ด์ค์ผ์ดํ ์ฒ๋ฆฌ
|
| 1033 |
+
from xml.sax.saxutils import escape
|
| 1034 |
+
safe_para = escape(para.replace('\n', '<br/>'))
|
| 1035 |
+
p = Paragraph(safe_para, normal_style)
|
| 1036 |
+
content.append(p)
|
| 1037 |
+
content.append(Spacer(1, 10))
|
| 1038 |
+
|
| 1039 |
+
# PDF ์์ฑ
|
| 1040 |
+
doc.build(content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1041 |
|
| 1042 |
+
# ํ์ผ๋ก ์ ์ฅ
|
| 1043 |
+
with open(file_path, 'wb') as f:
|
| 1044 |
+
f.write(pdf_buffer.getvalue())
|
| 1045 |
|
| 1046 |
# ๋ฉ์ธ ๋๋ ํ ๋ฆฌ์๋ ๋ณต์ฌ
|
| 1047 |
+
with open(PDF_DIR / filename, 'wb') as f:
|
| 1048 |
+
f.write(pdf_buffer.getvalue())
|
| 1049 |
|
| 1050 |
# PDF ID ์์ฑ ๋ฐ ๋ฉํ๋ฐ์ดํฐ ์ ์ฅ
|
| 1051 |
pdf_id = generate_pdf_id(filename)
|