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
Here is a typical use-case for displaying a PDF using class-based views:
For generating your own pdf with reportlab see the Django project Docs on PDF Generation.
Chris Pratt's response shows a good example of opening existing PDFs.
Browsers aren't PDF readers (unless they have the proper plugin/addon).
You may want to render the PDF as HTML instead, which can be done from the backend or the frontend.
I am just throwing this out there.
You can simply add your PDF resume to your static files.
If you are using White Noise to serve your static files, then you don't even need to make the view. Just then access your resume at the static location.
I added mine, here it is: https://www.jefferythewind.com/static/main/resume/TIm-D_Nice.pdf
Warning: This doesn't solve the
login_required
requirement in the questionIf you are working on a Windows machine pdf must be opened as
rb
notr
.