Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show a PDF file in a Django view?

Is it possible to show a PDF file in the Django view, rather than making the user have to download it to see it?

And if it is possible, how would it be done?

This is what I have so far -

@login_required
def resume(request, applicant_id):

    #Get the applicant's resume
    resume = File.objects.get(applicant=applicant_id)
    fsock = open(resume.location, 'r')
    response = HttpResponse(fsock, mimetype='application/pdf')

    return response
like image 238
SImon Avatar asked Aug 02 '12 14:08

SImon


People also ask

How do I view files in Django?

There is a file called urls.py on the myworld folder, open that file and add the include module in the import statement, and also add a path() function in the urlpatterns[] list, with arguments that will route users that comes in via 127.0. 0.1:8000/members/ . In the browser window, type 127.0.

How do I show a PDF on my website?

Navigate to the "Open With" option and choose "Chrome PDF Viewer" from the drop-down menu. You can also drag a PDF document directly into the browser, and it will open. Using this above outline method, opening a PDF document becomes easy. You can view a downloaded document directly using this method.


3 Answers

Django has a class specifically for returning files, FileResponse. It streams files, so that you don't have to read the entire file into memory before returning it. Here you go:

from django.http import FileResponse, Http404

def pdf_view(request):
    try:
        return FileResponse(open('foobar.pdf', 'rb'), content_type='application/pdf')
    except FileNotFoundError:
        raise Http404()

If you have really large files or if you're doing this a lot, a better option would probably be to serve these files outside of Django using normal server configuration.

like image 83
Flimm Avatar answered Oct 11 '22 12:10

Flimm


Simplistically, if you have a PDF file and you want to output it through a Django view, all you need to do is dump the file contents into the response and send it with the appropriate mimetype.

def pdf_view(request):
    with open('/path/to/my/file.pdf', 'r') as pdf:
        response = HttpResponse(pdf.read(), mimetype='application/pdf')
        response['Content-Disposition'] = 'inline;filename=some_file.pdf'
        return response
    pdf.closed

You can probably just return the response directly without specifying Content-Disposition, but that better indicates your intention and also allows you specify the filename just in case the user decides to save it.

Also, note that the view above doesn't handle the scenario where the file cannot be opened or read for whatever reason. Since it's done with with, it won't raise any exceptions, but you still must return some sort of response. You could simply raise an Http404 or something, though.

like image 34
Chris Pratt Avatar answered Oct 11 '22 10:10

Chris Pratt


PDF files must be opened as rb not r.

def pdf_view(request):
    with open('/path / to /name.pdf', 'rb') as pdf:
        response = HttpResponse(pdf.read(),content_type='application/pdf')
        response['Content-Disposition'] = 'filename=some_file.pdf'
        return response
like image 43
MKM Avatar answered Oct 11 '22 10:10

MKM