Flask将PDF作为自己的页面进行处理 [英] Flask handling a PDF as its own page

查看:583
本文介绍了Flask将PDF作为自己的页面进行处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的个人网站,我想为我的简历(PDF格式)另设一个页面。我已经尝试了多种方法,但我无法弄清楚如何让烧瓶处理PDF。



谢谢。让我知道如果我需要更好地解释我正在尝试做什么。

解决方案

对于这个问题的任何人来说,因为他们想用Flask从数据库提供PDF文件。将文件存储在数据库中时嵌入PDF不像在静态文件夹中那样简单。您必须使用 make_response 函数并为其提供适当的标题,以便浏览器知道如何处理二进制PDF数据,而不是像正常情况下那样从视图函数中返回。这里有一些伪代码可以帮助你:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ /< id>)
def get_pdf(id = None):
如果id不是None:
binary_pdf = get_binary_pdf_data_from_database(id = id)
response = make_response(binary_pdf )
response.headers ['Content-Type'] ='application / pdf'
response.headers ['Content-Disposition'] = \
'inline; filename =%s.pdf'%'yourfilename'
return response

您可以更改内容 - 如果您希望文件下载而不是显示在浏览器中,则从内联处置为附件。你也可以把这个视图放在一个子域中,例如docs.yourapp.com而不是yourapp.com/docs。最后一步是将这个文档实际嵌入到浏览器中。在任何页面上,只要使用rawrgulmuffin的策略即可:

 < embed src =/ docs / pdfid8676etcwidth = 500height =375> 

甚至可以用Jinja2模板创建src动态。我们把它放在doc.html中(注意大括号):

$ p $ < embed src =/ docs / {{doc_id }}>

然后,在视图函数中,您只需使用相应的doc_id返回呈现的模板:

  from flask import render_template 

@ app.route('/< id>')
def show_pdf(id = None):
如果id不是None:
return render_template('doc.html',doc_id = id)

用一个简单的GET请求嵌入用户从数据库请求的文档。希望这可以帮助任何人在数据库中处理大量的PDF!


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 PDF.

Thanks. Let me know if I need to better explain what I am trying to do.

解决方案

A note for anyone that came to this question because they're trying to serve PDF files from a database with Flask. Embedding a PDF when the file is stored on a database isn't as simple as when it's in the static folder. You have to use the make_response function and give it the appropriate headers so the browser knows what to do with your binary PDF data, rather than just returning it from the view function like normal. Here's some pseudocode to help:

from flask import make_response

@app.route('/docs/<id>')
def get_pdf(id=None):
    if id is not None:
        binary_pdf = get_binary_pdf_data_from_database(id=id)
        response = make_response(binary_pdf)
        response.headers['Content-Type'] = 'application/pdf'
        response.headers['Content-Disposition'] = \
            'inline; filename=%s.pdf' % 'yourfilename'
        return response

You can change the Content-Disposition from 'inline' to 'attachment' if you want the file to download rather than display in the browser. You could also put this view in a subdomain, e.g. docs.yourapp.com rather than yourapp.com/docs. The last step is to actually embed this document in the browser. On any page you'd like, just use rawrgulmuffin's strategy:

<embed src="/docs/pdfid8676etc" width="500" height="375">

You could even make the src dynamic with a Jinja2 template. Let's put this in doc.html (note the curly brackets):

<embed src="/docs/{{doc_id}}">

Then in a view function, you just return the rendered template with the appropriate doc_id:

from flask import render_template

@app.route('/<id>')
def show_pdf(id=None):
    if id is not None:
        return render_template('doc.html', doc_id=id)

This embeds a document the user requested from a database with a simple GET request. Hope this helps anyone working with lots of PDFs in a database!

这篇关于Flask将PDF作为自己的页面进行处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆