It seems that the default configuration for Wagtail CMS is to have links to documents trigger an automatic download of the document instead of displaying the document in the browser. Is there a simple way to change this configuration?
Following on from LB's accepted answer above, the following code will allow you to serve up a document to be viewed without relying on Google Document Viewer.
You can also revert to standard behaviour by passing a querystring parameter e.g. https://.../?download=True
from django.http import HttpResponse
from wagtail.core import hooks
@hooks.register('before_serve_document')
def serve_pdf(document, request):
if document.file_extension != 'pdf':
return # Empty return results in the existing response
response = HttpResponse(document.file.read(), content_type='application/pdf')
response['Content-Disposition'] = 'filename="' + document.file.name.split('/')[-1] + '"'
if request.GET.get('download', False) in [True, 'True', 'true']:
response['Content-Disposition'] = 'attachment; ' + response['Content-Disposition']
return response
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With