Spaces:
Running
Running
Commit
•
479eb2f
1
Parent(s):
6118db2
Upload 8 files
Browse files- .gitattributes +2 -0
- src/assets/InvitationTemplate.pdf +3 -0
- src/assets/InvitationTemplateCompressed.pdf +3 -0
- src/pyscripts/__init__.py +2 -0
- src/pyscripts/backup.py +42 -0
- src/pyscripts/file_process.py +24 -0
- src/pyscripts/oss.py +67 -0
- src/pyscripts/run.py +38 -0
- src/pyscripts/tempCodeRunnerFile.py +1 -0
.gitattributes
CHANGED
@@ -26,3 +26,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
26 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
27 |
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
28 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
26 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
27 |
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
28 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
29 |
+
src/assets/InvitationTemplate.pdf filter=lfs diff=lfs merge=lfs -text
|
30 |
+
src/assets/InvitationTemplateCompressed.pdf filter=lfs diff=lfs merge=lfs -text
|
src/assets/InvitationTemplate.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f2f0463b3a776395d96a64df9a43d4bc8117acf3e213be0759c2d71ee7a0d33c
|
3 |
+
size 5784474
|
src/assets/InvitationTemplateCompressed.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c7c7edc1d7019e5b2540e3b2d53fee9c91386249ff86f48e883ebbf3aa9963e7
|
3 |
+
size 5014682
|
src/pyscripts/__init__.py
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
def hello() -> str:
|
2 |
+
return "Hello from pyscripts!"
|
src/pyscripts/backup.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pdfrw
|
2 |
+
import io
|
3 |
+
import base64
|
4 |
+
|
5 |
+
ANNOT_KEY = '/Annots'
|
6 |
+
ANNOT_FIELD_KEY = '/T'
|
7 |
+
ANNOT_RECT_KEY = '/Rect'
|
8 |
+
SUBTYPE_KEY = '/Subtype'
|
9 |
+
WIDGET_SUBTYPE_KEY = '/Widget'
|
10 |
+
|
11 |
+
|
12 |
+
def write_fillable_pdf(input_pdf_path, output_pdf_path, data_dict):
|
13 |
+
"""填写pdf模板并输出新的pdf"""
|
14 |
+
template_pdf = pdfrw.PdfReader(input_pdf_path)
|
15 |
+
pdf_pages = len(template_pdf.pages)
|
16 |
+
template_pdf.Root.AcroForm.update(pdfrw.PdfDict(NeedAppearances=pdfrw.PdfObject('true')))
|
17 |
+
for pagenum in range(pdf_pages):
|
18 |
+
annotations = template_pdf.pages[pagenum][ANNOT_KEY]
|
19 |
+
if annotations is None:
|
20 |
+
continue
|
21 |
+
for annotation in annotations:
|
22 |
+
if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY:
|
23 |
+
if annotation[ANNOT_FIELD_KEY]:
|
24 |
+
key = annotation[ANNOT_FIELD_KEY][1:-1]
|
25 |
+
if key in data_dict.keys():
|
26 |
+
annotation.update(
|
27 |
+
pdfrw.PdfDict(V='{}'.format(data_dict[key]))
|
28 |
+
)
|
29 |
+
pdf_stream = io.BytesIO()
|
30 |
+
pdfrw.PdfWriter().write(pdf_stream, template_pdf)
|
31 |
+
base64.b64encode(pdf_stream.getvalue())
|
32 |
+
|
33 |
+
data_dict = {
|
34 |
+
'name': '宋有哲',
|
35 |
+
"conference": "世界教育者大会"
|
36 |
+
}
|
37 |
+
|
38 |
+
INVOICE_TEMPLATE_PATH = "src/assets/GuestPage.pdf"
|
39 |
+
INVOICE_OUTPUT_PATH = 'output/output.pdf'
|
40 |
+
|
41 |
+
if __name__ == '__main__':
|
42 |
+
write_fillable_pdf(INVOICE_TEMPLATE_PATH, INVOICE_OUTPUT_PATH, data_dict)
|
src/pyscripts/file_process.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import io
|
2 |
+
import fitz # PyMuPDF
|
3 |
+
|
4 |
+
pdf_path = "src/assets/InvitationTemplateCompressed.pdf"
|
5 |
+
|
6 |
+
def getInvitationPDF(data):
|
7 |
+
doc = fitz.open(pdf_path)
|
8 |
+
for page in doc:
|
9 |
+
fields = page.widgets()
|
10 |
+
for field in fields:
|
11 |
+
if field.field_name in data:
|
12 |
+
field.field_value = data[field.field_name]
|
13 |
+
field.update()
|
14 |
+
pdfStream = io.BytesIO()
|
15 |
+
doc.save(pdfStream)
|
16 |
+
doc.close()
|
17 |
+
return pdfStream
|
18 |
+
|
19 |
+
if __name__ == "__main__":
|
20 |
+
output_path = 'output/output.pdf'
|
21 |
+
data = {
|
22 |
+
'name': '宋',
|
23 |
+
"conference": "世界教育者大会"
|
24 |
+
}
|
src/pyscripts/oss.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import io
|
2 |
+
import os
|
3 |
+
import boto3
|
4 |
+
from botocore.exceptions import NoCredentialsError
|
5 |
+
|
6 |
+
ACCESS_KEY = os.environ.get('ACCESS_KEY')
|
7 |
+
SECRET_KEY = os.environ.get('SECRET_KEY')
|
8 |
+
ENDPOINT = os.environ.get("ENDPOINT")
|
9 |
+
BUCKET_NAME = 'host'
|
10 |
+
|
11 |
+
def create_s3_client(access_key, secret_key, endpoint_url):
|
12 |
+
try:
|
13 |
+
s3_client = boto3.client(
|
14 |
+
's3',
|
15 |
+
aws_access_key_id=access_key,
|
16 |
+
aws_secret_access_key=secret_key,
|
17 |
+
endpoint_url=endpoint_url
|
18 |
+
)
|
19 |
+
return s3_client
|
20 |
+
except NoCredentialsError:
|
21 |
+
print("Credentials not available")
|
22 |
+
return None
|
23 |
+
|
24 |
+
# 上传文件到 S3(或兼容 S3 的服务,如 IDrive e2)
|
25 |
+
def upload_file_stream_to_s3(file_stream, bucket_name, key):
|
26 |
+
s3_client = create_s3_client(ACCESS_KEY, SECRET_KEY, ENDPOINT)
|
27 |
+
file_stream.seek(0)
|
28 |
+
s3_client.upload_fileobj(
|
29 |
+
file_stream,
|
30 |
+
bucket_name,
|
31 |
+
key
|
32 |
+
)
|
33 |
+
# with open("output/output.pdf", 'wb') as f:
|
34 |
+
# file_stream.seek(0)
|
35 |
+
# f.write(file_stream.getvalue())
|
36 |
+
print(f"{key} has been uploaded to {bucket_name}")
|
37 |
+
return True
|
38 |
+
|
39 |
+
def download_file_stream_from_s3(bucket_name, key):
|
40 |
+
fileobj = io.BytesIO()
|
41 |
+
s3_client = create_s3_client(ACCESS_KEY, SECRET_KEY, ENDPOINT)
|
42 |
+
s3_client.download_fileobj(
|
43 |
+
bucket_name,
|
44 |
+
key,
|
45 |
+
fileobj,
|
46 |
+
)
|
47 |
+
return fileobj
|
48 |
+
|
49 |
+
|
50 |
+
# FILE_PATH = "src/assets/InvitationTemplate.pdf"
|
51 |
+
# OBJECT_NAME = 'test.pdf'
|
52 |
+
# ACCESS_KEY = os.environ.get('ACCESS_KEY')
|
53 |
+
# SECRET_KEY = os.environ.get('SECRET_KEY')
|
54 |
+
# ENDPOINT = os.environ.get("ENDPOINT")
|
55 |
+
# BUCKET_NAME = 'host'
|
56 |
+
# FILE_PATH = "src/assets/InvitationTemplate.pdf"
|
57 |
+
# OBJECT_NAME = 'test.pdf'
|
58 |
+
|
59 |
+
# # 创建 S3 客户端
|
60 |
+
# s3_client = create_s3_client(ACCESS_KEY, SECRET_KEY, ENDPOINT)
|
61 |
+
|
62 |
+
# # 上传文件
|
63 |
+
# if s3_client:
|
64 |
+
# # upload_file_to_s3(s3_client, FILE_PATH, BUCKET_NAME, OBJECT_NAME)
|
65 |
+
# fileobj = download_file_stream_from_s3(s3_client, BUCKET_NAME, OBJECT_NAME)
|
66 |
+
# with open(OBJECT_NAME, 'wb') as f:
|
67 |
+
# f.write(fileobj.getvalue())
|
src/pyscripts/run.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Path, Query
|
2 |
+
import gradio as gr
|
3 |
+
from src.pyscripts import file_process, oss
|
4 |
+
|
5 |
+
|
6 |
+
CUSTOM_PATH = "/gradio"
|
7 |
+
|
8 |
+
app = FastAPI()
|
9 |
+
|
10 |
+
pdf_path = "src/assets/InvitationTemplate.pdf"
|
11 |
+
output_path = 'output/output.pdf'
|
12 |
+
data = {
|
13 |
+
'name': '宋',
|
14 |
+
"conference": "世界教育者大会"
|
15 |
+
}
|
16 |
+
|
17 |
+
|
18 |
+
@app.get("/")
|
19 |
+
def read_main():
|
20 |
+
return {"message": "This is your main app"}
|
21 |
+
|
22 |
+
@app.post("/getInvitationPDF/{name}/{conference}")
|
23 |
+
def getInvitationPDF(name, conference):
|
24 |
+
data = {
|
25 |
+
'name': name,
|
26 |
+
"conference": conference
|
27 |
+
}
|
28 |
+
object_name = f"invitation/{name}_{conference}.pdf"
|
29 |
+
pdfStream = file_process.getInvitationPDF(data)
|
30 |
+
oss.upload_file_stream_to_s3(pdfStream, "host", object_name)
|
31 |
+
file_url = f"https://pub-429c75a96a8f4597984dd7ebc525d652.r2.dev/{object_name}"
|
32 |
+
# with open("output/output.pdf", 'wb') as f:
|
33 |
+
# f.write(pdfStream.getvalue())
|
34 |
+
return file_url
|
35 |
+
|
36 |
+
|
37 |
+
interface = gr.Interface(lambda x: "Hello, " + x + "!", "textbox", "textbox")
|
38 |
+
app = gr.mount_gradio_app(app, interface, path=CUSTOM_PATH)
|
src/pyscripts/tempCodeRunnerFile.py
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
output_path
|