Skip to content

Commit 5f261cf

Browse files
authored
Merge pull request #6 from team-MoPlus/develop
Fix: pdf buffer 생성
2 parents 5def2c9 + 41b75dd commit 5f261cf

File tree

7 files changed

+14
-2
lines changed

7 files changed

+14
-2
lines changed

.DS_Store

2 KB
Binary file not shown.

__pycache__/main.cpython-312.pyc

0 Bytes
Binary file not shown.

__pycache__/models.cpython-312.pyc

0 Bytes
Binary file not shown.
369 Bytes
Binary file not shown.

__pycache__/utils.cpython-312.pyc

0 Bytes
Binary file not shown.

output.pdf

-75.8 KB
Binary file not shown.

pdfmain.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import os, re
1+
import os, re, io
2+
from fastapi.responses import StreamingResponse
23
from reportlab.pdfgen import canvas
34
from reportlab.pdfbase import pdfmetrics
45
from reportlab.pdfbase.ttfonts import TTFont
@@ -10,14 +11,15 @@
1011
pattern = r"PT(?:(\d+)H)?(?:(\d+)M)?"
1112

1213
def create_review_note(data: DetailResultApplication):
14+
buffer = io.BytesIO()
1315
# 현재 파일의 디렉토리 경로를 기준으로 폰트 경로 설정
1416
# base_dir = os.path.dirname(__file__)
1517

1618
# 한글 폰트 등록
1719
pdfmetrics.registerFont(TTFont('Pretendard-Regular', "pdffonts/Pretendard-Regular.ttf"))
1820
pdfmetrics.registerFont(TTFont('Pretendard-Bold', "pdffonts/Pretendard-Bold.ttf"))
1921
pdfmetrics.registerFont(TTFont('Pretendard-Thin', "pdffonts/Pretendard-Thin.ttf"))
20-
c = canvas.Canvas("./output.pdf", pagesize=A4)
22+
c = canvas.Canvas(buffer, pagesize=A4)
2123
width, height = A4 # (595.2755905511812, 841.8897637795277)
2224

2325

@@ -296,3 +298,13 @@ def create_review_note(data: DetailResultApplication):
296298
page_num += 1
297299

298300
c.save()
301+
302+
# 버퍼의 시작 위치로 이동
303+
buffer.seek(0)
304+
305+
headers = {
306+
"Content-Disposition": "attachment; filename=download_test.pdf", # 파일명 설정
307+
}
308+
309+
# StreamingResponse로 PDF 반환
310+
return StreamingResponse(buffer, headers=headers, media_type="application/pdf")

0 commit comments

Comments
 (0)