Spaces:
Running
Running
Commit
•
5e8fcb1
1
Parent(s):
5b6d336
Update src/pyscripts/file_process.py
Browse files- src/pyscripts/file_process.py +62 -68
src/pyscripts/file_process.py
CHANGED
@@ -1,69 +1,63 @@
|
|
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 |
-
# @app.post("/getInvitationPDF/{name}/{title}/{conference}")
|
31 |
-
@app.get("/getInvitationPDF")
|
32 |
-
def getInvitationPDF(name, title, conference, category, respond_person):
|
33 |
-
person = Person(name=name, title=title, category=category, conference=conference, respond_person=respond_person)
|
34 |
-
formatted_date = datetime.now().strftime('%Y-%m-%d')
|
35 |
-
file_name = f"致{name+title}的WWEC2024邀请函.pdf"
|
36 |
-
file_dir = f"invitation/{category}"
|
37 |
-
object_name = file_dir + "/" + file_name
|
38 |
-
pdfStream = file_process.writeInvitationPDF(person)
|
39 |
-
# with open(f"output/{file_name}", 'wb') as f:
|
40 |
# f.write(pdfStream.getvalue())
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import io
|
2 |
+
import fitz # PyMuPDF
|
3 |
+
|
4 |
+
pdf_path_dict = {
|
5 |
+
"old": "src/assets/InvitationTemplateCompressed.pdf",
|
6 |
+
"common": "src/assets/WWEC嘉宾邀请函mini.pdf",
|
7 |
+
"观礼": "src/assets/WWEC列席嘉宾邀请函.pdf",
|
8 |
+
"演讲": "src/assets/WWEC演讲嘉宾邀请函.pdf",
|
9 |
+
"致辞": "src/assets/WWEC演讲嘉宾邀请函.pdf",
|
10 |
+
}
|
11 |
+
font_path = "src/assets/ZhouZiSongTi7000Zi-2.otf"
|
12 |
+
font_path = "src/assets/FangZhengHeiTiJianTi-1.ttf"
|
13 |
+
# font = fitz.Font(fontfile=font_path)
|
14 |
+
|
15 |
+
def getInvitationPDF(data):
|
16 |
+
pdf_path = pdf_path_dict[data["category"]]
|
17 |
+
doc = fitz.open(pdf_path)
|
18 |
+
for page in doc:
|
19 |
+
fields = page.widgets()
|
20 |
+
for field in fields:
|
21 |
+
if field.field_name in data:
|
22 |
+
# field.font = font
|
23 |
+
field.field_value = data[field.field_name]
|
24 |
+
field.update()
|
25 |
+
pdfStream = io.BytesIO()
|
26 |
+
doc.save(pdfStream)
|
27 |
+
doc.close()
|
28 |
+
# with open("output/output.pdf", 'wb') as f:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
# f.write(pdfStream.getvalue())
|
30 |
+
return pdfStream
|
31 |
+
|
32 |
+
|
33 |
+
def writeInvitationPDF(person, debug=False):
|
34 |
+
font_size=30
|
35 |
+
pdf_path = pdf_path_dict[person.category]
|
36 |
+
doc = fitz.open(pdf_path)
|
37 |
+
page = doc.load_page(1)
|
38 |
+
page.insert_font(fontname="f", fontfile=font_path)
|
39 |
+
page.insert_text((145, 80),
|
40 |
+
person.name+person.title, fontname="f", fontsize=min(font_size, font_size*5/len(person.name+person.title)), color=(1, 1, 1))
|
41 |
+
if person.category == "演讲" or person.category == "致辞":
|
42 |
+
page.insert_text((80, 375),
|
43 |
+
person.conference, fontname="f", fontsize=min(font_size, font_size*14/len(person.conference)), color=(1, 1, 1))
|
44 |
+
pdfStream = io.BytesIO()
|
45 |
+
doc.save(pdfStream)
|
46 |
+
doc.close()
|
47 |
+
if debug:
|
48 |
+
with open("output/output.pdf", 'wb') as f:
|
49 |
+
f.write(pdfStream.getvalue())
|
50 |
+
return pdfStream
|
51 |
+
|
52 |
+
|
53 |
+
if __name__ == "__main__":
|
54 |
+
from collections import namedtuple
|
55 |
+
Person = namedtuple('Person', ['name', 'title', 'category', 'conference', 'respond_person'])
|
56 |
+
person = Person(name="X专业测试XXX", title="先生", category="致辞", conference="8月21日中欧智慧论坛,做主题演讲", respond_person="Eazy")
|
57 |
+
|
58 |
+
output_path = 'output/output.pdf'
|
59 |
+
|
60 |
+
# getInvitationPDF(data)
|
61 |
+
writeInvitationPDF(person, debug=True)
|
62 |
+
# with open(output_path, 'wb') as f:
|
63 |
+
# f.write(pdfStream.getvalue())
|