Flask handling a PDF as its own page

前端 未结 5 1542
有刺的猬
有刺的猬 2020-12-14 08:52

For my personal website, I want to have a separate page just for my résumé, which is a PDF. I\'ve tried multiple ways, but I can\'t figure out how to get flask to handle a P

5条回答
  •  甜味超标
    2020-12-14 09:16

    You can use flask send_file or send_static_file function in 5 lines:

    from flask import send_file, current_app as app
    
    @app.route('/show/static-pdf/')
    def show_static_pdf():
        with open('/path/of/file.pdf', 'rb') as static_file:
            return send_file(static_file, attachment_filename='file.pdf')
    

    This snippet link is helpful

    Also can use send_from_directory if you want send file from certain directory:

    from flask import send_from_directory, current_app as app
    
    @app.route('/show/PDFs/')
    def send_pdf():
        return send_from_directory(app.config['UPLOAD_FOLDER'], 'file.pdf')
    

    read more about send_from_directory

提交回复
热议问题