whispersound commited on
Commit
8ec6c05
ยท
verified ยท
1 Parent(s): 1d790af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -27
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
- current_dir = os.path.dirname(os.path.abspath(__file__))
500
- font_path = os.path.join(current_dir, "NanumGothic.ttf")
 
 
 
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
- if not os.path.exists(font_path):
514
- raise FileNotFoundError(f"ํฐํŠธ ํŒŒ์ผ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค: {font_path}")
515
-
516
- # CSS ์Šคํƒ€์ผ ์ •์˜ (ํ•œ๊ธ€ ํฐํŠธ ์„ค์ •)
517
- css = CSS(string=f"""
518
- @font-face {{
519
- font-family: 'NanumGothic';
520
- src: url('{font_path}');
521
- font-weight: normal;
522
- font-style: normal;
523
- }}
524
- body, h1, h2, h3, p, li {{
525
- font-family: 'NanumGothic', sans-serif;
526
- line-height: 1.6;
527
- color: #333;
528
- }}
529
- """)
530
-
531
- # HTML ์ธ์ฝ”๋”ฉ ์„ค์ • ๋ฐ WeasyPrint ์‚ฌ์šฉํ•˜์—ฌ PDF๋กœ ๋ณ€ํ™˜ ๋ฐ ์ €์žฅ
532
- HTML(string=blog_post.encode('utf-8').decode('utf-8')).write_pdf(filename, stylesheets=[css])
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)}")