IsidoreSong commited on
Commit
5e8fcb1
1 Parent(s): 5b6d336

Update src/pyscripts/file_process.py

Browse files
Files changed (1) hide show
  1. src/pyscripts/file_process.py +62 -68
src/pyscripts/file_process.py CHANGED
@@ -1,69 +1,63 @@
1
- from fastapi import FastAPI, Path, Query, Response, BackgroundTasks
2
- from fastapi.responses import FileResponse, HTMLResponse
3
- import gradio as gr
4
- from src.pyscripts import file_process, oss, img_process
5
- from datetime import datetime
6
- from collections import namedtuple
7
- from pydantic import BaseModel
8
-
9
- Person = namedtuple('Person', ['name', 'title', 'category', 'conference', 'respond_person'])
10
-
11
- CUSTOM_PATH = "/gradio"
12
-
13
- app = FastAPI()
14
-
15
- pdf_path = "src/assets/InvitationTemplate.pdf"
16
- output_path = 'output/output.pdf'
17
- person = Person(name="雷军", title="先生", category="观礼", conference="世界教育者大会", respond_person="Eazy")
18
-
19
- # data = {
20
- # 'name': '雷军',
21
- # 'title': '先生',
22
- # "conference": "世界教育者大会"
23
- # }
24
-
25
-
26
- @app.get("/")
27
- def read_main():
28
- return {"message": "This is your main app"}
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
- oss.upload_file_stream_to_s3(pdfStream, "host", object_name)
42
- file_url = f"https://isidoresong.us.kg/{object_name}"
43
- return {"url": file_url}
44
-
45
- class ExhibitorPosterInfo(BaseModel):
46
- exhibitor: str
47
- logo_link: str
48
- slogan: str
49
- content: str
50
-
51
- @app.post("/getExhibitorPoster")
52
- def getExhibitorPoster(item: ExhibitorPosterInfo):
53
- logo_link = item.logo_link
54
- exhibitor = item.exhibitor
55
- slogan = item.slogan
56
- content = item.content
57
- img_stream = img_process.make_exhibitor_poster(logo_link, slogan, content)
58
- file_dir = "ExhibitorPoster"
59
- object_name = file_dir + "/" + exhibitor + ".jpg"
60
- # with open(f"output/{exhibitor + '.jpg'}", 'wb') as f:
61
- # f.write(img_stream.getvalue())
62
- oss.upload_file_stream_to_s3(img_stream, "host", object_name)
63
- file_url = f"https://isidoresong.us.kg/{object_name}"
64
- return {"url": file_url}
65
-
66
- interface = gr.Interface(lambda x: "Hello, " + x + "!", "textbox", "textbox")
67
- app = gr.mount_gradio_app(app, interface, path=CUSTOM_PATH)
68
-
69
- # if __name__ == "__main__":
 
 
 
 
 
 
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())