How to make flask to send a file and then redirect?
You can make the "/" path return a html response with javascript like:
<!DOCTYPE html><html lang="en"><head> <script> setTimeout(function(){ document.location = "/redirect-uri"; }, 500) </script></head><body> <iframe width="0" height="0" src="/path-to-pdf.pdf"/></body></html>
If the pdf is dynamically generated, you should also serve the pdf url like
@route('/path-to-pdf.pdf')def pdf_generator(): return create_pdf()
A slightly better way might be to let javascript handle submitting the form as well as the redirect:
<form name="createPdf"> <!-- Your fields etc. here --> <a href="javascript:submitAndRedirect()"> <button>Submit</button> <!-- NB do not use type=submit --> </a></form><script type="text/javascript"> function submitAndRedirect() { document.createPdf.submit(); setTimeout(function() { location.href = YOUR_REDIRECT_URL_HERE; }, 500); }</script>
The redirectUrl could be injected from python if necessary like this:
return render_template("create_pdf.html", redirect_url=url_for("whatever"))
Then in your javascript:
var redirectUrl = '{{ redirect_url }}';