Update main.py
Browse files
main.py
CHANGED
|
@@ -112,18 +112,19 @@ async def convert_to_pdf(request: HTMLRequest):
|
|
| 112 |
raise HTTPException(status_code=500, detail=str(e))
|
| 113 |
|
| 114 |
|
|
|
|
|
|
|
| 115 |
from html4docx import HtmlToDocx
|
| 116 |
-
import io
|
| 117 |
-
import tempfile
|
| 118 |
import os
|
| 119 |
|
|
|
|
| 120 |
class HTMLInput(BaseModel):
|
| 121 |
html: str
|
| 122 |
|
| 123 |
# Define the path to the temporary folder
|
| 124 |
TEMP_FOLDER = "/app/temp"
|
| 125 |
|
| 126 |
-
@app.post("/
|
| 127 |
async def convert_html_to_docx(input_data: HTMLInput):
|
| 128 |
temp_filename = None
|
| 129 |
try:
|
|
@@ -144,14 +145,18 @@ async def convert_html_to_docx(input_data: HTMLInput):
|
|
| 144 |
file_contents = file.read()
|
| 145 |
|
| 146 |
# Return the DOCX file as a response
|
| 147 |
-
return
|
| 148 |
content=file_contents,
|
| 149 |
media_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
| 150 |
-
headers={"Content-Disposition": "attachment; filename=
|
| 151 |
)
|
| 152 |
except Exception as e:
|
| 153 |
raise HTTPException(status_code=500, detail=str(e))
|
| 154 |
finally:
|
| 155 |
# Clean up: remove the temporary file
|
| 156 |
if temp_filename and os.path.exists(temp_filename):
|
| 157 |
-
os.remove(temp_filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
raise HTTPException(status_code=500, detail=str(e))
|
| 113 |
|
| 114 |
|
| 115 |
+
from fastapi import FastAPI, HTTPException, Response
|
| 116 |
+
from pydantic import BaseModel
|
| 117 |
from html4docx import HtmlToDocx
|
|
|
|
|
|
|
| 118 |
import os
|
| 119 |
|
| 120 |
+
|
| 121 |
class HTMLInput(BaseModel):
|
| 122 |
html: str
|
| 123 |
|
| 124 |
# Define the path to the temporary folder
|
| 125 |
TEMP_FOLDER = "/app/temp"
|
| 126 |
|
| 127 |
+
@app.post("/convert")
|
| 128 |
async def convert_html_to_docx(input_data: HTMLInput):
|
| 129 |
temp_filename = None
|
| 130 |
try:
|
|
|
|
| 145 |
file_contents = file.read()
|
| 146 |
|
| 147 |
# Return the DOCX file as a response
|
| 148 |
+
return Response(
|
| 149 |
content=file_contents,
|
| 150 |
media_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
| 151 |
+
headers={"Content-Disposition": "attachment; filename=converted.docx"}
|
| 152 |
)
|
| 153 |
except Exception as e:
|
| 154 |
raise HTTPException(status_code=500, detail=str(e))
|
| 155 |
finally:
|
| 156 |
# Clean up: remove the temporary file
|
| 157 |
if temp_filename and os.path.exists(temp_filename):
|
| 158 |
+
os.remove(temp_filename)
|
| 159 |
+
|
| 160 |
+
if __name__ == "__main__":
|
| 161 |
+
import uvicorn
|
| 162 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|