File size: 1,030 Bytes
d2e693e 23fbfed 80624f7 d2e693e 80624f7 d2e693e 80624f7 d2e693e 80624f7 d2e693e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
from fastapi import APIRouter, HTTPException
from fpdf import FPDF
from fastapi.responses import FileResponse
import os
router = APIRouter()
# Rota para gerar um PDF a partir de texto
@router.post("/pdf/generate/")
async def generate_pdf(content: str):
"""
Gera um PDF a partir do texto fornecido.
"""
try:
# Cria um novo PDF
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
# Adiciona conteúdo ao PDF
pdf.multi_cell(0, 10, content)
# Salva o PDF temporariamente
pdf_file = "generated_pdf.pdf"
pdf.output(pdf_file)
return FileResponse(
pdf_file,
headers={"Content-Disposition": f"attachment; filename={pdf_file}"}
)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error generating PDF: {str(e)}")
finally:
# Remove o arquivo temporário após envio
if os.path.exists(pdf_file):
os.remove(pdf_file) |