Update routers/summarizer.py
Browse files- routers/summarizer.py +42 -19
routers/summarizer.py
CHANGED
@@ -1,36 +1,59 @@
|
|
1 |
from fastapi import APIRouter, HTTPException
|
2 |
-
from fpdf import FPDF
|
3 |
from fastapi.responses import FileResponse
|
4 |
import os
|
|
|
|
|
5 |
|
6 |
router = APIRouter()
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
14 |
try:
|
15 |
-
# Cria um novo PDF
|
16 |
pdf = FPDF()
|
17 |
pdf.add_page()
|
18 |
pdf.set_font("Arial", size=12)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
#
|
21 |
-
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
-
|
26 |
|
|
|
27 |
return FileResponse(
|
28 |
-
|
29 |
-
|
|
|
30 |
)
|
|
|
|
|
|
|
|
|
31 |
except Exception as e:
|
32 |
-
raise HTTPException(status_code=500, detail=f"
|
|
|
33 |
finally:
|
34 |
-
# Remove o arquivo tempor谩rio ap贸s envio
|
35 |
-
if os.path.exists(
|
36 |
-
os.remove(
|
|
|
1 |
from fastapi import APIRouter, HTTPException
|
|
|
2 |
from fastapi.responses import FileResponse
|
3 |
import os
|
4 |
+
from datetime import datetime
|
5 |
+
from fpdf import FPDF # Certifique-se de que essa biblioteca est谩 instalada
|
6 |
|
7 |
router = APIRouter()
|
8 |
|
9 |
+
# Caminho absoluto para o diret贸rio de arquivos tempor谩rios
|
10 |
+
TEMP_DIR = os.path.abspath("temp_files")
|
11 |
+
os.makedirs(TEMP_DIR, exist_ok=True) # Garante que o diret贸rio exista
|
12 |
+
|
13 |
+
|
14 |
+
# Fun莽茫o para gerar um PDF
|
15 |
+
def generate_pdf(file_path: str):
|
16 |
try:
|
|
|
17 |
pdf = FPDF()
|
18 |
pdf.add_page()
|
19 |
pdf.set_font("Arial", size=12)
|
20 |
+
pdf.cell(200, 10, txt="Exemplo de PDF Gerado com FastAPI", ln=True, align='C')
|
21 |
+
pdf.cell(200, 10, txt=f"Data e Hora: {datetime.now()}", ln=True, align='C')
|
22 |
+
|
23 |
+
pdf.output(file_path)
|
24 |
+
except Exception as e:
|
25 |
+
raise HTTPException(status_code=500, detail=f"Erro ao gerar PDF: {str(e)}")
|
26 |
+
|
27 |
+
|
28 |
+
# Rota para baixar o PDF
|
29 |
+
@router.get("/download-pdf")
|
30 |
+
async def download_pdf():
|
31 |
+
try:
|
32 |
+
# Caminho absoluto do arquivo PDF
|
33 |
+
file_name = "generated_pdf.pdf"
|
34 |
+
file_path = os.path.join(TEMP_DIR, file_name)
|
35 |
|
36 |
+
# Gera o PDF
|
37 |
+
generate_pdf(file_path)
|
38 |
|
39 |
+
# Verifica se o arquivo existe
|
40 |
+
if not os.path.exists(file_path):
|
41 |
+
raise HTTPException(status_code=500, detail="O arquivo PDF n茫o foi gerado corretamente.")
|
42 |
|
43 |
+
# Retorna o arquivo PDF
|
44 |
return FileResponse(
|
45 |
+
path=file_path,
|
46 |
+
filename="download.pdf",
|
47 |
+
media_type="application/pdf"
|
48 |
)
|
49 |
+
|
50 |
+
except HTTPException as http_err:
|
51 |
+
raise http_err
|
52 |
+
|
53 |
except Exception as e:
|
54 |
+
raise HTTPException(status_code=500, detail=f"Erro inesperado: {str(e)}")
|
55 |
+
|
56 |
finally:
|
57 |
+
# Remove o arquivo tempor谩rio ap贸s o envio
|
58 |
+
if os.path.exists(file_path):
|
59 |
+
os.remove(file_path)
|