I am trying to download a PDF file using the send_file method, the request is executed successfully and returns code 200, but nothing is downloaded. I used the same procedure that I have in another project running, I don't know what is going wrong.
This is the code I am trying to run
index.html
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Solicitar firma de usuario - By Parzibyte</title>
<link rel="stylesheet" href="/static/estilo.css">
</head>
<body>
<p>Firmar a continuación:</p>
<canvas id="canvas"></canvas>
<br>
<button id="btnLimpiar">Limpiar</button>
<button id="btnDescargar">Descargar</button>
<button id="btnGenerarDocumento">Pasar a documento</button>
<br>
<a href="https://parzibyte.me/blog">By Parzibyte</a>
<script src="/static/script.js"></script>
<script>
$btnDescargar.onclick = async () => {
// Convertir la imagen a Base64 y ponerlo en el enlace
const data = $canvas.toDataURL("image/png");
const fd = new FormData();
fd.append("imagen", data);
const respuestaHttp = await fetch("{{url_for('generar')}}", {
method: "POST",
body: fd,
});
//const nombreImagenSubida = await JSON.parse(respuestaHttp);
//console.log("La imagen ha sido enviada y tiene el nombre de: " + nombreImagenSubida);
};
</script>
</body>
</html>
main.py
from flask import Flask, redirect, render_template, send_file, url_for, request
from generar_firma import convertir_a_imagen
from escribirExcel import firmar_excel
from deExcelaPDF import convertir_excel_a_pdf
import time
import os
app = Flask(__name__)
app.config['SECRET_KEY'] = ''
@app.route('/')
def index():
return render_template("index.html")
@app.route('/api/generar', methods=["GET","POST"])
def generar():
#La firma llega en forma de string y la recibo aca
image_string = request.form['imagen']
#Se la paso a un metodo del modulo gener_firma, que la convierte de string a base64
#y de base64 a png
#En firma solamente guardo el nombre del archivo para despues buscarlo
firma = convertir_a_imagen(image_string)
#Llamo a este metodo de escribirExcel.py que se encarga de firmar el Excel
#esto me devuelve la ruta del excel firmado
nombre_excel_firmado = firmar_excel(firma)
print(f'nombre_excel_firmado: {nombre_excel_firmado}')
#paso la ruta del excel firmado a un metodo del modulo deExcelaPDF
filename = convertir_excel_a_pdf(nombre_excel_firmado)
directorio = os.path.join(app.root_path)
print(f'ruta_pdf: {directorio}\{filename}')
return send_file(path_or_file=directorio+'\\'+filename, as_attachment=True)
if __name__ == "__main__":
app.run(debug=True)
#app.run(host='0.0.0.0', port='8383')
The console output
ruta_pdf: C:\app-flask\pruebaFirmada.pdf
127.0.0.1 - - [31/Mar/2022 12:20:30] "POST /api/generar HTTP/1.1" 200 -
The PDF exists in the directory indicated by the print in console.