Skip to content

Commit

Permalink
Set content-disposition on invoice pdf viewers
Browse files Browse the repository at this point in the history
When viewing a PDF either in the user or admin interface, set the
filename using content-disposition so if the user decides to save the
file it will get a reasonable filename, and not just `document.pdf` or
similar depending on the browser.
  • Loading branch information
mhagander committed Nov 23, 2023
1 parent f4d94bc commit 19b0e30
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions postgresqleu/invoices/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,13 +411,15 @@ def viewinvoicepdf(request, invoiceid):
authenticate_backend_group(request, 'Invoice managers')

r = HttpResponse(content_type='application/pdf')
r['Content-disposition'] = 'filename={}_invoice_{}.pdf'.format(settings.INVOICE_FILENAME_PREFIX, invoice.id)
r.write(base64.b64decode(invoice.pdf_invoice))
return r


def viewinvoicepdf_secret(request, invoiceid, invoicesecret):
invoice = get_object_or_404(Invoice, pk=invoiceid, recipient_secret=invoicesecret)
r = HttpResponse(content_type='application/pdf')
r['Content-disposition'] = 'filename={}_invoice_{}.pdf'.format(settings.INVOICE_FILENAME_PREFIX, invoice.id)
r.write(base64.b64decode(invoice.pdf_invoice))
return r

Expand All @@ -430,13 +432,15 @@ def viewreceipt(request, invoiceid):
authenticate_backend_group(request, 'Invoice managers')

r = HttpResponse(content_type='application/pdf')
r['Content-disposition'] = 'filename={}_receipt_{}.pdf'.format(settings.INVOICE_FILENAME_PREFIX, invoice.id)
r.write(base64.b64decode(invoice.pdf_receipt))
return r


def viewreceipt_secret(request, invoiceid, invoicesecret):
invoice = get_object_or_404(Invoice, pk=invoiceid, recipient_secret=invoicesecret)
r = HttpResponse(content_type='application/pdf')
r['Content-disposition'] = 'filename={}_receipt_{}.pdf'.format(settings.INVOICE_FILENAME_PREFIX, invoice.id)
r.write(base64.b64decode(invoice.pdf_receipt))
return r

Expand All @@ -451,6 +455,7 @@ def viewrefundnote(request, invoiceid, refundid):
refund = get_object_or_404(InvoiceRefund, invoice=invoiceid, pk=refundid)

r = HttpResponse(content_type='application/pdf')
r['Content-disposition'] = 'filename={}_refund_{}.pdf'.format(settings.INVOICE_FILENAME_PREFIX, invoice.id)
r.write(base64.b64decode(refund.refund_pdf))
return r

Expand All @@ -459,6 +464,7 @@ def viewrefundnote_secret(request, invoiceid, invoicesecret, refundid):
invoice = get_object_or_404(Invoice, pk=invoiceid, recipient_secret=invoicesecret)
refund = get_object_or_404(InvoiceRefund, invoice=invoice, pk=refundid)
r = HttpResponse(content_type='application/pdf')
r['Content-disposition'] = 'filename={}_refund_{}.pdf'.format(settings.INVOICE_FILENAME_PREFIX, invoice.id)
r.write(base64.b64decode(refund.refund_pdf))
return r

Expand Down

0 comments on commit 19b0e30

Please sign in to comment.