-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshirtificate.py
More file actions
87 lines (66 loc) · 2.45 KB
/
shirtificate.py
File metadata and controls
87 lines (66 loc) · 2.45 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
"""
Suppose that you’d like to implement a CS50 “shirtificate,” a PDF with an image
of an I took CS50 t-shirt, shirtificate.png, customized with a user’s own name.
In a file called shirtificate.py, implement a program that prompts the user for
their name and outputs, using fpdf2, a CS50 shirtificate in a file called
shirtificate.pdf similar to this one for John Harvard, with these
specifications:
The orientation of the PDF should be Portrait.
The format of the PDF should be A4, which is 210mm wide by 297mm tall.
The top of the PDF should say “CS50 Shirtificate” as text, centered
horizontally.
The shirt’s image should be centered horizontally.
The user’s name should be on top of the shirt, in white text.
All other details we leave to you. You’re even welcome to add borders, colors,
and lines. Your shirtificate needn’t match John Harvard’s precisely. And no need
to wrap long names across multiple lines.
Before writing any code, do read through fpdf2’s tutorial to learn how to use
it. Then skim fpdf2’s API (application programming interface) to see all of its
functions and parameters therefor.
No need to submit any PDFs with your code. But, if you would like, you’re
welcome (but not expected) to share a shirtificate with your name on it in any
of CS50’s communities!
"""
from fpdf import FPDF, enums
class PDF(FPDF):
def header(self):
# performing a line break:
self.ln(20)
# setting font: helvetica bold 50
self.set_font("helvetica", "", 50)
# printing title:
self.cell(
w=0,
h=10,
text=self.title,
new_x="LMARGIN",
new_y="NEXT",
align="C",
)
# performing a line break:
self.ln(30)
def main():
name = str(input("Name: ")).strip().title()
shirtificate = "CS50 Shirtificate"
name = f"{name} took CS50"
# initial steps
pdf = PDF()
pdf.set_auto_page_break(False)
pdf.set_title(shirtificate)
pdf.add_page()
# add shirt image
pdf.image(
name="shirtificate.png",
x=enums.Align.C,
w=pdf.epw,
)
# setting font and font color
pdf.set_font("helvetica", "", 20)
pdf.set_text_color(255, 255, 255)
# place text in shirt image
text_width = pdf.get_string_width(name) + 6
pdf.text(x=(210 - text_width) / 2, y=140, text=name)
# output file
pdf.output("shirtificate.pdf")
if __name__ == "__main__":
main()