Search code examples
djangopython-3.xtemplatesiframedjango-2.1

Django display a pdf in an iframe


Currently I am Working on a workflow where on the left side are input fields and on the right half is a scalable pdf.

But actually I am not able to show a PDF in an iframe.

I am using django 2.1.1(python 3.7.2) on windows. Is there any solution to display a pdf in an iframe?

I hope you have some ideas..i looked up many sides an tried different solution but actually the pdf document is not shown.. Hope to hear from you soon!

Kind regards


Solution

  • I have experienced a similar setting where I wanted to render a PDF in a webpage. For that, I did the following : Wrote a view method (in views.py) that renders the pdf's name. Indicated in the urls.py the path used for the view method. Next, in the HTML file, I use the following line of code :

    <iframe id="iframe_pdf" src="{{ doc_file.url }}" 
          style="width:800px; height:800px;" frameborder="0"></iframe>
    

    The {{ doc_file.url }} is the data obtained from the View method, it returns the PDF file location as a string. In my case, the pdfs were stored in the MEDIA_ROOT directory set in settings.py.