python - send pdf as bytes in web services python - send pdf as bytes in web services flask flask

python - send pdf as bytes in web services


You can use send_file or send_from_directory:

from flask import send_from_directory@app.route('/pdf/myfile')def pdf():    return send_from_directory('/dir/of/pdf', 'my.pdf')

By default this will send the file inline and browsers will probably render the PDF itself. If you set as_attachment=True the file will be presented as an attachment, and the browser will throw up a "save as" dialog box.

send_file gives you more control over things such as mime types and caching. The defaults should work well.