Spaces:
Running
Running
whispersound
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -495,43 +495,65 @@ def format_filename(text):
|
|
495 |
|
496 |
def save_to_pdf(blog_post, user_topic):
|
497 |
try:
|
498 |
-
|
499 |
-
|
500 |
-
|
|
|
|
|
|
|
501 |
|
502 |
# ํ์ฌ ๋ ์ง์ ์๊ฐ์ ๊ฐ์ ธ์ต๋๋ค (๋ํ๋ฏผ๊ตญ ์๊ฐ ๊ธฐ์ค)
|
503 |
now = datetime.now(ZoneInfo("Asia/Seoul"))
|
504 |
date_str = now.strftime("%y%m%d")
|
505 |
time_str = now.strftime("%H%M")
|
506 |
-
|
507 |
# ์ฒซ ๋ฒ์งธ ์ ๋ชฉ์ ์ฐพ์ ํ์ผ๋ช
์ผ๋ก ์ฌ์ฉ
|
508 |
title_match = re.search(r'<h[1-3][^>]*>(.*?)</h[1-3]>', blog_post)
|
509 |
title = title_match.group(1) if title_match else "Untitled"
|
510 |
filename = f"{date_str}_{time_str}_{format_filename(title)}.pdf"
|
511 |
-
|
512 |
-
#
|
513 |
-
|
514 |
-
|
515 |
-
|
516 |
-
|
517 |
-
|
518 |
-
|
519 |
-
|
520 |
-
|
521 |
-
|
522 |
-
|
523 |
-
|
524 |
-
|
525 |
-
|
526 |
-
|
527 |
-
|
528 |
-
|
529 |
-
|
530 |
-
|
531 |
-
|
532 |
-
|
533 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
534 |
print(f"Saving PDF as: {filename}")
|
|
|
535 |
return filename
|
536 |
except Exception as e:
|
537 |
print(f"PDF ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|
|
|
495 |
|
496 |
def save_to_pdf(blog_post, user_topic):
|
497 |
try:
|
498 |
+
pdf = PDF()
|
499 |
+
pdf.add_page()
|
500 |
+
pdf.set_auto_page_break(auto=True, margin=15)
|
501 |
+
|
502 |
+
# HTML ํ๊ทธ๋ฅผ ํ์ฑํ๊ธฐ ์ํ ์ ๊ทํํ์
|
503 |
+
tag_pattern = re.compile(r'<(/?)(\w+)([^>]*)>')
|
504 |
|
505 |
# ํ์ฌ ๋ ์ง์ ์๊ฐ์ ๊ฐ์ ธ์ต๋๋ค (๋ํ๋ฏผ๊ตญ ์๊ฐ ๊ธฐ์ค)
|
506 |
now = datetime.now(ZoneInfo("Asia/Seoul"))
|
507 |
date_str = now.strftime("%y%m%d")
|
508 |
time_str = now.strftime("%H%M")
|
509 |
+
|
510 |
# ์ฒซ ๋ฒ์งธ ์ ๋ชฉ์ ์ฐพ์ ํ์ผ๋ช
์ผ๋ก ์ฌ์ฉ
|
511 |
title_match = re.search(r'<h[1-3][^>]*>(.*?)</h[1-3]>', blog_post)
|
512 |
title = title_match.group(1) if title_match else "Untitled"
|
513 |
filename = f"{date_str}_{time_str}_{format_filename(title)}.pdf"
|
514 |
+
|
515 |
+
# HTML ๋ด์ฉ์ ์ํํ๋ฉฐ PDF์ ์์ฑ
|
516 |
+
current_tag = ''
|
517 |
+
buffer = ''
|
518 |
+
is_bold = False
|
519 |
+
|
520 |
+
for part in re.split(tag_pattern, blog_post):
|
521 |
+
if part in ['h1', 'h2', 'h3', 'p', 'strong', 'li', 'br']:
|
522 |
+
if buffer:
|
523 |
+
if current_tag in ['h1', 'h2', 'h3']:
|
524 |
+
pdf.set_font("NanumGothic", 'B', 16 if current_tag == 'h1' else 14)
|
525 |
+
pdf.multi_cell(0, 10, buffer.strip(), align='L')
|
526 |
+
pdf.ln(5)
|
527 |
+
elif current_tag == 'p':
|
528 |
+
pdf.set_font("NanumGothic", '', 11)
|
529 |
+
pdf.multi_cell(0, 6, buffer.strip(), align='J')
|
530 |
+
pdf.ln(5)
|
531 |
+
elif current_tag == 'li':
|
532 |
+
pdf.set_font("NanumGothic", '', 11)
|
533 |
+
pdf.multi_cell(0, 6, "โข " + buffer.strip(), align='J')
|
534 |
+
elif current_tag == 'br':
|
535 |
+
pdf.ln(5)
|
536 |
+
buffer = ''
|
537 |
+
current_tag = part
|
538 |
+
elif part == 'strong':
|
539 |
+
is_bold = True
|
540 |
+
pdf.set_font("NanumGothic", 'B', 11)
|
541 |
+
elif part == '/strong':
|
542 |
+
is_bold = False
|
543 |
+
pdf.set_font("NanumGothic", '', 11)
|
544 |
+
elif part.startswith('/') or part == 'div':
|
545 |
+
continue
|
546 |
+
elif not tag_pattern.match(part) and part.strip():
|
547 |
+
pdf.write(6, part.strip() + ' ')
|
548 |
+
|
549 |
+
# ๋ง์ง๋ง ๋ฒํผ ์ฒ๋ฆฌ
|
550 |
+
if buffer:
|
551 |
+
pdf.set_font("NanumGothic", '', 11)
|
552 |
+
pdf.multi_cell(0, 6, buffer.strip(), align='J')
|
553 |
+
|
554 |
+
# PDF ์ ์ฅ
|
555 |
print(f"Saving PDF as: {filename}")
|
556 |
+
pdf.output(filename, 'F')
|
557 |
return filename
|
558 |
except Exception as e:
|
559 |
print(f"PDF ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|