• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

How to Render and Open PDF file in Django?

Aniruddha Chaudhari/2745/0
Misc

Interestingly to render PDF file in Django you don’t need to create HTML template.

In this tutorial, we are going to setting up the static PDF file and render and allow user to downlow PDF file.

There are two ways to access the static file in the Django Python script. One is using static() method. Another is using os.path.join() method.

Here is the twist. You can not use the static file path you get from static() method to open the file. If you use it, you will get an error saying…

FileNotFoundError
Exception Value:
[Errno 2] No such file or directory: '/static/sample.pdf'

So, use later one.

Here is the sample code to set a view (view.py) to render and open PDF file in Django.

from django.http import FileResponse
import os

def show_pdf(request):
	filepath = os.path.join('static', 'sample.pdf')
	return FileResponse(open(filepath, 'rb'), content_type='application/pdf')

Some of the points you should remember.

  • Django view function returns the FileResponse. You need to import it from django.http before using.
  • Open file in the ‘rb’ mode using Python open() method.
  • You have to provide the complete or relative path to your PDF file.
  • FileResponse content-type should be ‘application/pdf’.
  • Make sure, you have a PDF file present in your project static directory.

It is not good practice to hardcode the file path. File path of the static files can vary while deploying production build.

If you are getting FileNotFoundError, check this tutorial where I have described steps to debug this issue.

Note: If you have a download manager installed on your system, it will start downloading PDF file rather than showing it in the browser. If you want to prevent users from downloading or saving PDFs, this is not a recommended solution.

There are many use cases to open pdf file in Django. For example, if you are building an eCommerce website, with this technique you can allow users to download the invoices for purchased products and services. You can also allow users to download the specific documentation files.

There are different methods to open PDF file in Django based on the use case. Here we have discussed the simplest one. If you have any queries, let me know in the comment.

Django
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

Prerequisite for Django

Learn Python Basics

Django Tutorials

Why Django?

Django- Create Your First Project

Django- Adding CSS/Static Files

Django- Use/Load Static Files

Django- Create Form

Django- Create Input-Tags

Django- Display Message on Form Submit

Django- MultipleChoiceField in Form

Django- Read Model Data (order_by)

Django- Read First/Last Queryset Object

Django- Update Model Field

Django- Signals

Django- Create TextField in Form

Django- Add Model to Admin Site

Django- Customize Admin Site

Django- Understanding Model User

Django- Registration/Signup Form

Django- Form Layout with Widget-Tweaks

Django- OneToOneField in Model

Django- Foreign Key & Reverse Lookup

Django- Custom Template Filters

Django- Interactive Shell

Django- Social Login

Django- Deploy and Host [FREE] 🔥

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard