Skip to content

Commit

Permalink
Merge pull request #33 from Ph0tonic/main
Browse files Browse the repository at this point in the history
Allow filename with non ascii characters
  • Loading branch information
liZe authored Nov 24, 2024
2 parents cf5bec5 + 50ced41 commit 580e83d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
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

0 comments on commit 580e83d

Please sign in to comment.