Hello,

Maybe you could use ImageMagick to show one page of the pdf as an image ? 
This is what I have implemented on my project : first page of pdf files can 
be pre-viewed before user uploads the whole file (if needed). In your case, 
pre-viewing could be parametrized to show page ā€˜n’ of the pdf file, 
probably. In my project, pre-viewing image is generated when pdf file is 
uploaded and saved by admin in myproject/media/files directory. Once pdf 
files are in media directory, users can preview those pdf files and tnen 
upload them, if needed (only first page can be pre-viewed by user. This is 
an implementation choice).


http://www.imagemagick.org/script/index.php


<http://www.imagemagick.org/script/index.php>

Implementation principles/credit can be found here :

http://www.yaconiello.com/blog/auto-generating-pdf-covers/


<http://www.yaconiello.com/blog/auto-generating-pdf-covers/>

I am attaching the signals file_post_save file_pre_save, for information.


Cheers.

Django 1.8
Python 3.4


Note : to install ImageMagick:

sudo apt-get install libmagickwand-dev imagemagick libmagickcore-dev

The convert command allows to convert any page of pdf file into png image 
(for instance), eg :

convert -thumbnail 1280 test.pdf[0] test_1280.png
Several parameters exist to tune image quality (in my case, need to use 
density, trim and quality).

 

*models.p**y*

[…]

###########

# SIGNALS #

###########

from django.db.models.signals import post_save, pre_delete

from django.dispatch import receiver

from myproject.settings import MEDIA_ROOT

import subprocess


# What to do after a File is saved - receiver definition

def file_post_save(sender, instance=False, **kwargs):

            # This post save function creates a thumbnail for the File

            file = File.objects.get(pk=instance.pk)

            command = "convert -density 300 -trim -thumbnail %s %s%s[0] 
-quality 100 %s%s" % (file.thumbnail_size, MEDIA_ROOT, file.file, 
MEDIA_ROOT, file.thumbnail)

            proc = subprocess.Popen(command,

                        shell=True,

                        stdin=subprocess.PIPE,

                        stdout=subprocess.PIPE,

                        stderr=subprocess.PIPE,

            )

            stdout_value = proc.communicate()[0]

post_save.connect(file_post_save, sender=File, 
dispatch_uid="file_post_save_uid")

 

# What to do before a File is deleted - receiver definition

def file_pre_delete(sender, instance=False, **kwargs):

            # This pre delete function deletes file and thumbnail from 
media directory

            file = File.objects.get(pk=instance.pk)

            command = "rm %s%s %s%s" % (MEDIA_ROOT, file.file, MEDIA_ROOT, 
file.thumbnail)

            proc = subprocess.Popen(command,

                        shell=True,

                        stdin=subprocess.PIPE,

                        stdout=subprocess.PIPE,

                        stderr=subprocess.PIPE,

            )

            stdout_value = proc.communicate()[0]

pre_delete.connect(file_pre_delete, sender=File, 
dispatch_uid="file_pre_delete_uid")

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/e409aafa-3133-47b1-b465-7d74501d0cac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to