Skip to content

Commit 5d6ea51

Browse files
committed
Stream PDFs and docx fiels with FileResponse etc.
1 parent f81b42c commit 5d6ea51

2 files changed

Lines changed: 14 additions & 13 deletions

File tree

hypha/apply/projects/views/project.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from django.core.exceptions import ObjectDoesNotExist, PermissionDenied
1212
from django.db import transaction
1313
from django.db.models import Q
14-
from django.http import Http404, HttpResponse, HttpResponseRedirect
14+
from django.http import FileResponse, Http404, HttpResponse, HttpResponseRedirect
1515
from django.shortcuts import get_object_or_404, render
1616
from django.template.loader import get_template
1717
from django.urls import reverse
@@ -1915,10 +1915,11 @@ def render_as_docx(self, context, template, filename):
19151915
new_parser = HtmlToDocx()
19161916
new_parser.add_html_to_document(html, document)
19171917
document.save(buf)
1918+
buf.seek(0)
19181919

1919-
response = HttpResponse(buf.getvalue(), content_type="application/docx")
1920-
response["Content-Disposition"] = f"attachment; filename={filename}"
1921-
return response
1920+
return FileResponse(
1921+
buf, as_attachment=True, filename=filename, content_type="application/docx"
1922+
)
19221923

19231924
def get_slugified_file_name(self, export_type):
19241925
return f"{datetime.date.today().strftime('%Y%m%d')}-{slugify(self.object.title)}.{export_type}"
@@ -1983,10 +1984,11 @@ def render_as_docx(self, context, template, filename):
19831984
new_parser = HtmlToDocx()
19841985
new_parser.add_html_to_document(html, document)
19851986
document.save(buf)
1987+
buf.seek(0)
19861988

1987-
response = HttpResponse(buf.getvalue(), content_type="application/docx")
1988-
response["Content-Disposition"] = f"attachment; filename={filename}"
1989-
return response
1989+
return FileResponse(
1990+
buf, as_attachment=True, filename=filename, content_type="application/docx"
1991+
)
19901992

19911993
def get_slugified_file_name(self, export_type):
19921994
return f"{datetime.date.today().strftime('%Y%m%d')}-{slugify(self.object.title)}.{export_type}"

hypha/apply/utils/pdfs.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from io import BytesIO
22

33
from django.core.files import File
4-
from django.http import HttpResponse
4+
from django.http import FileResponse, HttpResponse
55
from django.template.loader import render_to_string
66
from django.utils import timezone
77
from pypdf import PdfReader, PdfWriter
@@ -59,10 +59,9 @@ def render_as_pdf(
5959
)
6060
pdf = html_to_pdf(html)
6161

62-
response = HttpResponse(content_type="application/pdf")
63-
response["Content-Disposition"] = f"attachment; filename={filename}"
64-
response.write(pdf.read())
65-
return response
62+
return FileResponse(
63+
pdf, as_attachment=True, filename=filename, content_type="application/pdf"
64+
)
6665

6766

6867
def merge_pdf(origin_pdf: BytesIO, input_pdf: BytesIO) -> File:
@@ -76,7 +75,7 @@ def merge_pdf(origin_pdf: BytesIO, input_pdf: BytesIO) -> File:
7675
Return a File object containing the merged PDF and with the same name as the
7776
original PDF.
7877
"""
79-
merger = PdfWriter(clone_from=BytesIO(origin_pdf.read()))
78+
merger = PdfWriter(clone_from=origin_pdf)
8079
merger.append(PdfReader(input_pdf))
8180

8281
output_pdf = BytesIO()

0 commit comments

Comments
 (0)