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

Allow filename with non ascii characters #33

Merged
merged 3 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions flask_weasyprint/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Make PDF in your Flask app with WeasyPrint."""

from io import BytesIO
from urllib.parse import urljoin, urlsplit

from flask import current_app, has_request_context, request
from flask import current_app, has_request_context, request, send_file
from werkzeug.test import Client, ClientRedirectError, EnvironBuilder
from werkzeug.wrappers import Response

Expand Down Expand Up @@ -197,10 +198,7 @@ def render_pdf(html, stylesheets=None, download_filename=None,
if not hasattr(html, 'write_pdf'):
html = HTML(html)
pdf = html.write_pdf(stylesheets=stylesheets, **options)
response = current_app.response_class(pdf, mimetype='application/pdf')
if download_filename:
response.headers.add(
'Content-Disposition',
'attachment' if automatic_download else 'inline',
filename=download_filename)
return response
as_attachment = automatic_download if download_filename else False
return send_file(
BytesIO(pdf), mimetype="application/pdf", as_attachment=as_attachment,
download_name=download_filename)
7 changes: 4 additions & 3 deletions tests/test_flask_weasyprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ def test_pdf(url, filename, automatic, cookie):
response = render_pdf(HTML(string=document_html()), **options)
assert response.status_code == 200
assert response.mimetype == 'application/pdf'
assert response.data.startswith(b'%PDF')
data = b''.join(response.iter_encoded())
assert data.startswith(b'%PDF')
if cookie:
assert cookie.encode() in response.data
assert b'/URI (https://courtbouillon.org/)' in response.data
assert cookie.encode() in data
assert b'/URI (https://courtbouillon.org/)' in data
disposition = response.headers.get('Content-Disposition')
if filename:
position = 'attachment' if automatic else 'inline'
Expand Down