Initial commit
This commit is contained in:
2
.gitattributes
vendored
Normal file
2
.gitattributes
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
# Auto detect text files and perform LF normalization
|
||||||
|
* text=auto
|
BIN
__pycache__/app.cpython-312.pyc
Normal file
BIN
__pycache__/app.cpython-312.pyc
Normal file
Binary file not shown.
27
app.py
Normal file
27
app.py
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
from fastapi import FastAPI
|
||||||
|
from fastapi.staticfiles import StaticFiles
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from reportlab.pdfgen import canvas
|
||||||
|
import uuid, os
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
os.makedirs("static", exist_ok=True)
|
||||||
|
app.mount("/static", StaticFiles(directory="static"), name="static")
|
||||||
|
|
||||||
|
class TextRequest(BaseModel):
|
||||||
|
content: str
|
||||||
|
|
||||||
|
@app.post("/generate-pdf")
|
||||||
|
def generate_pdf(data: TextRequest):
|
||||||
|
filename = f"{uuid.uuid4()}.pdf"
|
||||||
|
filepath = os.path.join("static", filename)
|
||||||
|
|
||||||
|
c = canvas.Canvas(filepath)
|
||||||
|
c.setFont("Helvetica", 12)
|
||||||
|
c.drawString(100, 750, data.content[:1000]) # 控制最多幾個字,否則會 overflow
|
||||||
|
c.save()
|
||||||
|
|
||||||
|
return {
|
||||||
|
"url": f"/static/{filename}"
|
||||||
|
}
|
5
requirements.txt
Normal file
5
requirements.txt
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
fastapi
|
||||||
|
uvicorn
|
||||||
|
pydantic
|
||||||
|
reportlab
|
||||||
|
python-multipart
|
Reference in New Issue
Block a user