ginipick commited on
Commit
a4f7568
ยท
verified ยท
1 Parent(s): de7b0b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -50
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
- # ๊ธฐ๋ณธ Helvetica ํฐํŠธ ์‚ฌ์šฉ (ํ•œ๊ธ€ ์ง€์› ์—†์Œ)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
975
  from reportlab.lib.pagesizes import letter
976
- from reportlab.pdfgen import canvas
 
 
 
 
 
 
 
 
 
 
 
 
 
 
977
 
978
- # PDF ์ƒ์„ฑ
979
- c = canvas.Canvas(str(file_path), pagesize=letter)
 
 
 
 
 
 
 
980
 
981
- # ํŽ˜์ด์ง€ ํฌ๊ธฐ ์„ค์ •
982
- page_width, page_height = letter
983
 
984
  # ์ œ๋ชฉ ์ถ”๊ฐ€
985
- c.setFont("Helvetica-Bold", 16)
986
- c.drawCentredString(page_width/2, page_height - 50, title)
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 not para.strip():
998
- continue
999
-
1000
- # ๋‹จ๋ฝ ๋‚ด ์ค„ ๋ฐ”๊ฟˆ ์ฒ˜๋ฆฌ
1001
- lines = para.split('\n')
1002
- for line in lines:
1003
- # ํ•œ ์ค„์˜ ์ตœ๋Œ€ ๋ฌธ์ž ์ˆ˜
1004
- max_chars_per_line = 80
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
- # PDF ์ €์žฅ
1031
- c.save()
 
1032
 
1033
  # ๋ฉ”์ธ ๋””๋ ‰ํ† ๋ฆฌ์—๋„ ๋ณต์‚ฌ
1034
- shutil.copy2(file_path, PDF_DIR / filename)
 
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)