Django- Display PDf document in an Iframe or Pdf VIewer

1,505 views
Skip to first unread message

Lucas Gärtner

unread,
Nov 25, 2019, 9:46:23 AM11/25/19
to Django users
Hello everyone!

I just signed up here, so maybe this thread is wrong in this group.

The problem is as follows:

I need to display a PDF document in a half of a web application.
I tried it first with Iframe and now today with pdf.js.
Nothing helped. Do you have any idea?
It is only showing the pdf, not more.

I am using django: 2.1.10 and Python 3.7.1

Hope you have some good recommendations!

Have a nice day!
Lucas

Mark Phillips

unread,
Nov 25, 2019, 10:43:29 AM11/25/19
to django users
Lucas,

I have many documents that I have uploaded to my django app. The docs are stored in the file system, but the path is in my django model. The docs are either pdfs or png images. 

This is how I am displaying pdf images in my ModelAdmin change form:

  readonly_fields = ('get_image_3', )

    def get_image_3(self, obj):
        display_image = ""
        if (obj.document_id):
            # Get the path to the main image
            doc_file_path = obj.document_id.storage_file_name.path
            if obj.document_id.storage_file_name.name.endswith('pdf'):
                display_image = '<embed src="{image_link}" width="620px" height="420px" alt="pdf" pluginspage="http://www.adobe.com/products/acrobat/readstep2.html">'.format(image_link = obj.document_id.storage_file_name.url)      
            else:
                display_image = '<div><b>Click to tag people, Ctrl-click to tag animals</b></div><img src="{document_link}" id="img1" doc_id="{doc_id}" style="height:{height}"/>'.format(document_link = obj.document_id.storage_file_name.url, doc_id=obj.document_id.document_id, height=settings.FACE_ADMIN_PREVIEW_HEIGHT)
        return format_html(display_image)


The adobe reader renders a nice window for scrolling the pdf and changing pages. 

To display a thumbnail of the pdf on the main ModelAdmin page, I used Pillow to grab the first page of the pdf and make a jpg thumbnail of that page. Then use similar code from the display_image above after the else (for displaying an image on the page in the admin).

Hope that helps!

Mark

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8304b914-3861-4c33-8948-87f9baeaf27f%40googlegroups.com.

13515...@qq.com

unread,
Dec 3, 2019, 7:28:31 PM12/3/19
to django-users
hi:
1、the pdf file is static file,u can write the url in html to show it.
2、if u want to show it in half of web page,u can use <div> with fixed width and height besides iframe.


wblueboat
--
Reply all
Reply to author
Forward
0 new messages