Model (PDF to Image)

Hello!

In my app, via post form I receive a PDF file.

models.py

class UploadFiles(models.Model):
    pdf = models.FileField(upload_to='pdf/')

In my views I’ve been able to convert the PDF file to images using the pdf2image library and storing the converted images in any folder I want. Now, I want to make the conversion in models so that I can store the path of the converted image in database and the images in “media/pdf/” (for further loop calls in templates).

But I’m confused about how to do this. What I’ve tried so far has been this:

#views.py

def converter(request):
    
    if request.method == 'POST':
        form = UploadFilesForm(request.POST ,request.FILES )
        
        if form.is_valid():
            pdf = form.save()
            pdf.convert()
          
    else:
        form = UploadFilesForm()
    
    context = {'form':form}
    return render(request, 'converter.html',context)

I’m having trouble specifically in the models.py section.

models.py

class UploadFiles(models.Model):                                  
    pdf = models.FileField(upload_to='pdf/')

    def convert(self):                              
        
      # Convert pdf to an image ( filename = converted image)
       
        UploadFiles.create(filename,'JPEG')
        self.save()

How should I save the converted images in my models? Is this the correct way to do it or should I save the converted images in other model somehow?

Thanks.

Are you keeping both the image and the original uploaded pdf? Or are you looking to keep only the image file?

If you’re only keeping the image file, I would recommend doing that processing in the view, working with the temporary file created by the upload process. (Django doesn’t automatically “clean up” unreferenced files uploaded into the media directory.)

I would like to keep only the image file. I think I’m working now with the temporary file since I am not saving the form yet but I’m able to convert the PDF to a PNG image and store it. However, I’m still confused about uploading the converted image into my models. Here’s what I’ve done:

models.py

class Files(models.Model):
    archivo = models.FileField(upload_to='pdf/')
    

views.py

from django.core.files.storage import default_storage
from django.core.files.base import ContentFile

def converter(request):
    
    if request.method == 'POST':
        form = UploadFilesForm(request.POST ,request.FILES )
        if form.is_valid():
            new_pdf = request.FILES['archivo']
            path = default_storage.save("file.pdf", ContentFile(new_pdf.read()))   
            path = settings.MEDIA_ROOT  + '/' + "file.pdf"
            images = convert_from_path(path,fmt='png',dpi=300,grayscale=True)
            image = images[0]
            image.save("imagen.png")
            
            # form.save(image)   How can I save the image in my models? 
    else:
        form = UploadFilesForm()
    
    context = {'form':form}
    return render(request, 'converter.html',context)

What could I be missing?

Thank you Sr.