Flask handling a PDF as its own page

前端 未结 5 1547
有刺的猬
有刺的猬 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:28

    I know it's quite late, but I just found the easiest way and it's not among those.

    Just redirect to the file's path as a static file, with any hyperlink:

    "/docs/sample.pdf" #the browser will take care about showing it
    

    Or, if you want a maybe cleaner way... I just made a flask page for the pdfs like this:

    @app.route('/pdf/') #the url you'll send the user to when he wants the pdf
    def pdfviewer():
        return redirect("/docs/sample.pdf") #the pdf itself
    

    ...which I guess is kinda like giving a variable name to the pdf link (?), and also should be easier to adapt if I need to send the user to different pdfs depending on a variable.

    I'm thinking something in this lines:

    @app.route('/pdf') #passing variable through the url
    def pdfviewer(number):
        if number == 1: #pdf depending on that variable
            return redirect("/docs/sample1.pdf")
        elif number == 2:
            return redirect("/docs/sample1.pdf")
        ...
        else:
            return "Hey I don't have such pdf" #basic html page as default
    

    BTW: I'm working on pythonAnywhere, so I had to first define the path to the static files in the Web tab, so I could show static files to the user.

提交回复
热议问题