Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using download_filename in render_pdf changes functionality #8

Closed
SirNeuman opened this issue May 8, 2015 · 4 comments
Closed

using download_filename in render_pdf changes functionality #8

SirNeuman opened this issue May 8, 2015 · 4 comments

Comments

@SirNeuman
Copy link

Previously I used:
Python:

APP.route("/to-pdf/<string:id>")
def to_pdf(id):
    ...
    html = render_template('pdf.html', id=id)
    return render_pdf(HTML(string=html))

HTML: <a href="/to-pdf/{{id}}" target="_blank">

And it worked as expected. Upon clicking the a-tag the browser would display the pdf in a new tab. However I decided that i wanted a specific filename for when users downloaded the pdf from the browser. so i changed the Python to:

APP.route("/to-pdf/<string:id>")
def to_pdf(id):
    ...
    html = render_template('pdf.html', id=id)
    pdf_filename = "PDF-" + id
    return render_pdf(HTML(string=html), download_filename=pdf_filename)

As per the flask-weasyprint documentation. Now, however, upon clicking the a-tag a new tab does not open, and it just downloads directly to the computer without a preview. It still generates the same pdf, but I would like it to keep the functionality as before with the addition of a default custom filename. So basically I'm wondering how i can set the default filename for downloading a file, without changing the url and while still allowing the pdf to be viewable in the browser (i.e. not download directly to the computer). Thanks

@SimonSapin
Copy link
Member

That’s not how browsers work. The download_filename="foo" parameters makes Flask-WeasyPrint add the Content-Disposition: attachment; filename="foo" header to the HTTP response. It’s that attachment value that makes browsers download a file rather than show it themselves, and the file name can not be specified without it.

Without a Content-Disposition header, browsers typically infer a file name from the last component of the path of the URL. You could have you Flask routes chosen accordingly. For example:

APP.route("/to-pdf/PDF-<string:id>.pdf")
def to_pdf(id):
    ...

By the way, you could use something like {{ url_for('to_pdf', id=id) }} in your HTML template to avoid hard-coding URLs.

@SirNeuman
Copy link
Author

Ok thanks. In this case i actually cant use the url_for() since my id is actually set via javascript, unless there's some flask stuff i'm not aware of that lets you do that. I just used that flask-set-id in my example for simplicity.

Anyway, now i know a little bit more about how browsers work.

@SimonSapin
Copy link
Member

url_for might not be the answer for your exact case, but you should still be able to change the URL path to fit the desired file name.

@wiltonsr
Copy link
Contributor

wiltonsr commented Mar 9, 2019

Hello, everyone

It would be a good option to add an automatic_download boolean parameter. If it were False, the line could have the inline value instead of the attachment.

This way the behavior would be exactly what @SirNeuman needs. If the user clicks to download, the file's name would have the desired name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants