-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdfmain.py
More file actions
341 lines (265 loc) · 12.8 KB
/
pdfmain.py
File metadata and controls
341 lines (265 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import os, re, io
from fastapi.responses import StreamingResponse
from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.pagesizes import A4
from reportlab.lib.colors import HexColor
from models import DetailResultApplication
from utils import draw_dashed_box, draw_heading, draw_page_number, draw_problem_page, draw_vertical_orange
pattern = r"PT(?:(\d+)H)?(?:(\d+)M)?"
def create_review_note(data: DetailResultApplication, file_name: str, buffer):
# 한글 폰트 등록
pdfmetrics.registerFont(TTFont('Pretendard-Regular', "pdffonts/Pretendard-Regular.ttf"))
pdfmetrics.registerFont(TTFont('Pretendard-Bold', "pdffonts/Pretendard-Bold.ttf"))
pdfmetrics.registerFont(TTFont('Pretendard-Thin', "pdffonts/Pretendard-Thin.ttf"))
c = canvas.Canvas(buffer, pagesize=A4)
width, height = A4 # (595.2755905511812, 841.8897637795277)
# 색상 설정
orange_color = HexColor('#FC6C02')
lightgrey_color = HexColor('#D9D9D9')
grey_color = HexColor('#828282')
black_color = HexColor("#333333")
draw_heading(c, orange_color, lightgrey_color)
draw_page_number(c, "1")
##### 결과 #####
# 세로선 그리기 (오렌지 색상)
draw_vertical_orange(c, 22, height - 100, 22, height - 130, orange_color) # 세로선 좌표
c.setFillColor(black_color)
c.setFont("Pretendard-Bold", 24)
c.drawString(30, height - 123, "결과") # 텍스트 위치 설정
# 점수
c.setFillColor(orange_color)
c.setFont("Pretendard-Regular", 80)
c.drawString(35, height - 220, f'{data.score}')
score_width = c.stringWidth(f'{data.score}', "Pretendard-Regular", 80)
c.setFillColor(black_color)
c.setFont("Pretendard-Regular", 60)
c.drawString(35 + score_width, height - 217, "점")
c.setFillColor(grey_color)
c.setFont("Pretendard-Regular", 20)
c.drawString(164, height - 248, "점수")
# 내 등급
c.setFillColor(orange_color)
c.setFont("Pretendard-Regular", 40)
c.drawString(245, height - 220, f'{data.estimatedRatingGetResponses[0].estimatedRating}')
c.setFillColor(black_color)
c.setFont("Pretendard-Regular", 30)
c.drawString(270, height - 220, "등급")
c.setFillColor(grey_color)
c.setFont("Pretendard-Regular", 20)
c.drawString(280, height - 246, "내 등급")
# 내 풀이 시간
match = re.match(pattern, data.solvingTime)
h, m = f'{match.group(1) if match and match.group(1) else 0}', f'{match.group(2) if match and match.group(2) else 0}'
c.setFillColor(orange_color)
c.setFont("Pretendard-Regular", 40)
c.drawString(392, height - 220, h)
c.drawString(440, height - 220, m)
c.setFillColor(black_color)
c.setFont("Pretendard-Regular", 36)
h_width = c.stringWidth(h, "Pretendard-Regular", 40)
c.drawString(392+h_width, height - 220, "h")
m_width = c.stringWidth(m, "Pretendard-Regular", 40)
c.drawString(440+m_width, height - 220, "m")
c.setFillColor(grey_color)
c.setFont("Pretendard-Regular", 20)
c.drawString(418, height - 246, "내 풀이 시간")
##### 전체 틀린 문제 #####
# 세로선 그리기 (오렌지 색상)
draw_vertical_orange(c, 22, height - 284, 22, height - 314, orange_color) # 세로선 좌표
c.setFillColor(black_color)
c.setFont("Pretendard-Bold", 24)
c.drawString(30, height - 307, "전체 틀린 문제") # 텍스트 위치 설정
## 문제들
text_y = 500
if len(data.incorrectProblems) <= 10:
for i, problem in enumerate(data.incorrectProblems):
if i%5 == 0 and i > 0:
text_y -= 27
c.setFillColor(black_color)
c.setFont("Pretendard-Regular", 16)
p_num = f'{problem.problemNumber}번'
c.drawString(25 + (i%5)*105, text_y, p_num)
p_num_width = c.stringWidth(p_num, "Pretendard-Regular", 20)
box_width = 40
box_height = 16
box_x = 23 + (i%5)*105 + p_num_width
box_y = text_y - 3
# 텍스트 그리기
c.setFont("Pretendard-Regular", 10) # 글꼴과 크기 설정
c.setFillColor(orange_color)
c.drawString(box_x + 5, box_y + 4, f'{int(problem.correctRate)}%') # 박스 안 텍스트 위치 조정
rate_width = c.stringWidth(f'{int(problem.correctRate)}%', "Pretendard-Regular", 10)
# 박스 그리기 (배경)
c.setLineWidth(1) # 테두리 두께 설정 (얇게)
c.setStrokeColor(HexColor("#FFA500")) # 주황색 테두리
c.roundRect(box_x+1, box_y, rate_width + 7, box_height, radius=4, stroke=1, fill=0)
else:
for i, problem in enumerate(data.incorrectProblems):
if i%5 == 0 and i > 0:
text_y -= 20
c.setFillColor(black_color)
c.setFont("Pretendard-Regular", 10)
p_num = f'{problem.problemNumber}번'
c.drawString(25 + (i%5)*105, text_y+10, p_num)
p_num_width = c.stringWidth(p_num, "Pretendard-Regular", 14)
box_width = 40
box_height = 12
box_x = 24 + (i%5)*105 + p_num_width
box_y = text_y + 7
# 텍스트 그리기
c.setFont("Pretendard-Regular", 8) # 글꼴과 크기 설정
c.setFillColor(orange_color)
c.drawString(box_x + 6, box_y + 4, f'{int(problem.correctRate)}%') # 박스 안 텍스트 위치 조정
rate_width = c.stringWidth(f'{int(problem.correctRate)}%', "Pretendard-Regular", 8)
# 박스 그리기 (배경)
c.setLineWidth(1) # 테두리 두께 설정 (얇게)
c.setStrokeColor(HexColor("#FFA500")) # 주황색 테두리
c.roundRect(box_x+2, box_y, rate_width + 8, box_height, radius=4, stroke=1, fill=0)
# 점선 상자1
draw_dashed_box(c, 30, 175, width - 60, 255)
### 현재 등급에서 맞췄어야 하는 문제 ###
# 세로선 그리기 (오렌지 색상)
c.setDash([])
draw_vertical_orange(c, 40, 425, 40, 400, orange_color) # 세로선 좌표
c.setFillColor(black_color)
c.setFont("Pretendard-Bold", 18)
c.drawString(48, 406, "현재 등급에서 맞췄어야 하는 문제") # 텍스트 위치 설정
c.setFillColor(grey_color)
text = c.beginText(53, 380)
text.setFont("Pretendard-Regular", 12)
text.setTextOrigin(53, 380)
text.textLine("현재 틀린 문제 중 현재 등급에서 다음 등급으로 넘어가기 위해서 반드시 공부하고")
text.textLine("넘어가야하는 문제들이예요.")
c.drawText(text)
if len(data.forCurrentRating) == 0:
c.setFillColor(HexColor("#95E0BB"))
c.setFont("Pretendard-Bold", 18)
c.drawString(52, 338, "모두 맞았어요!")
else:
text_y = 335
for i, problem in enumerate(data.forCurrentRating):
if i%5 == 0 and i > 0:
text_y -= 30
c.setFillColor(black_color)
c.setFont("Pretendard-Regular", 14)
p_num = f'{problem.problemNumber}번'
c.drawString(45 + (i%5)*105, text_y, p_num)
p_num_width = c.stringWidth(p_num, "Pretendard-Regular", 14)
box_width = 40
box_height = 20
box_x = 50 + (i%5)*105 + p_num_width
box_y = text_y - 4
rate_width = c.stringWidth(f'{int(problem.correctRate)}%', "Pretendard-Regular", 12)
# 텍스트 그리기
c.setFont("Pretendard-Regular", 12) # 글꼴과 크기 설정
c.setFillColor(orange_color)
c.drawString(box_x + 4, box_y + 5, f'{int(problem.correctRate)}%') # 박스 안 텍스트 위치 조정
# 박스 그리기 (배경)
c.setLineWidth(1) # 테두리 두께 설정 (얇게)
c.setStrokeColor(HexColor("#FFA500")) # 주황색 테두리
c.roundRect(box_x, box_y, rate_width + 8, box_height, radius=4, stroke=1, fill=0)
### 다음 등급을 위해 맞춰야 하는 문제 ###
# 세로선 그리기 (오렌지 색상)
c.setDash([])
draw_vertical_orange(c, 40, 291, 40, 266, orange_color) # 세로선 좌표
c.setFillColor(black_color)
c.setFont("Pretendard-Bold", 18)
c.drawString(48, 272, "다음 등급을 위해 맞춰야 하는 문제") # 텍스트 위치 설정
c.setFillColor(grey_color)
text = c.beginText(53, 246)
text.setFont("Pretendard-Regular", 12)
text.setTextOrigin(53, 246)
text.textLine("다음 등급에서 맞춰야하는 문제들이예요.")
c.drawText(text)
if len(data.forNextRating) == 0:
c.setFillColor(HexColor("#95E0BB"))
c.setFont("Pretendard-Bold", 18)
c.drawString(52, 210, "모두 맞았어요!")
else:
text_y = 220
for i, problem in enumerate(data.forNextRating):
if i%5 == 0 and i > 0:
text_y -= 30
c.setFillColor(black_color)
c.setFont("Pretendard-Regular", 14)
p_num = f'{problem.problemNumber}번'
c.drawString(45 + (i%5)*105, text_y, p_num)
p_num_width = c.stringWidth(p_num, "Pretendard-Regular", 14)
box_width = 40
box_height = 20
box_x = 50 + (i%5)*105 + p_num_width
box_y = text_y - 4
rate_width = c.stringWidth(f'{int(problem.correctRate)}%', "Pretendard-Regular", 12)
# 텍스트 그리기
c.setFont("Pretendard-Regular", 12) # 글꼴과 크기 설정
c.setFillColor(orange_color)
c.drawString(box_x + 4, box_y + 5, f'{int(problem.correctRate)}%') # 박스 안 텍스트 위치 조정
# 박스 그리기 (배경)
c.setLineWidth(1) # 테두리 두께 설정 (얇게)
c.setStrokeColor(HexColor("#FFA500")) # 주황색 테두리
c.roundRect(box_x, box_y, rate_width + 8, box_height, radius=4, stroke=1, fill=0)
# 점선 상자2
draw_dashed_box(c, 30, 30, width - 60, 140)
### 등급 다지기 ###
# 세로선 그리기 (오렌지 색상)
c.setDash([])
draw_vertical_orange(c, 40, 161, 40, 136, orange_color) # 세로선 좌표
c.setFillColor(black_color)
c.setFont("Pretendard-Bold", 18)
c.drawString(48, 142, "등급 다지기") # 텍스트 위치 설정
c.setFillColor(grey_color)
text = c.beginText(53, 119)
text.setFont("Pretendard-Regular", 12)
text.setTextOrigin(53, 119)
text.textLine("현재 등급 대비 난이도가 낮은 문제예요. 다시 한번 풀어보세요.")
c.drawText(text)
if len(data.forBeforeRating) == 0:
c.setFillColor(HexColor("#95E0BB"))
c.setFont("Pretendard-Bold", 18)
c.drawString(52, 83, "모두 맞았어요!")
else:
text_y = 80
for i, problem in enumerate(data.forBeforeRating):
if i%5 == 0 and i > 0:
text_y -= 30
c.setFillColor(black_color)
c.setFont("Pretendard-Regular", 14)
p_num = f'{problem.problemNumber}번'
c.drawString(45 + (i%5)*105, text_y, p_num)
p_num_width = c.stringWidth(p_num, "Pretendard-Regular", 14)
box_width = 40
box_height = 20
box_x = 50 + (i%5)*105 + p_num_width
box_y = text_y - 4
rate_width = c.stringWidth(f'{int(problem.correctRate)}%', "Pretendard-Regular", 12)
# 텍스트 그리기
c.setFont("Pretendard-Regular", 12) # 글꼴과 크기 설정
c.setFillColor(orange_color)
c.drawString(box_x + 4, box_y + 5, f'{int(problem.correctRate)}%') # 박스 안 텍스트 위치 조정
# 박스 그리기 (배경)
c.setLineWidth(1) # 테두리 두께 설정 (얇게)
c.setStrokeColor(HexColor("#FFA500")) # 주황색 테두리
c.roundRect(box_x, box_y, rate_width + 8, box_height, radius=4, stroke=1, fill=0)
page_num = 2
for problem_data in data.forCurrentRating:
c.showPage()
draw_problem_page(c, data=problem_data, page_number=page_num, flag=0)
page_num += 1
for problem_data in data.forNextRating:
c.showPage()
draw_problem_page(c, data=problem_data, page_number=page_num, flag=1)
page_num += 1
for problem_data in data.forBeforeRating:
c.showPage()
draw_problem_page(c, data=problem_data, page_number=page_num, flag=2)
page_num += 1
c.save()
# 버퍼의 시작 위치로 이동
buffer.seek(0)
headers = {
f"Content-Disposition": "attachment; filename={file_name}.pdf", # 파일명 설정
}
# StreamingResponse로 PDF 반환
return StreamingResponse(buffer, headers=headers, media_type="application/pdf")