0

Currently working on a django social media application where next to posting every post to the feed, the information of each upload should create a pdf document, containing caption, image, created_at and image_id.

I´ve put the canvas into the upload functions, so that both will be created on the same click. So far, i get a pdf (can't get the attributes from the post into the pdf tho) and the post is uploaded just fine. How do I get the posted data into the pdf? And how do save that pdf to a folder within the project instead of starting an automatic download? The user should not be able to notice the pdf converting. It is just for back office - but very necessary due to the social media website being a part of a big installation. So how do I get these pdfs?

Here is the code:

views.py

def upload(request):
    if request.method == 'POST':
        #user = request.user.username
        image = request.FILES.get('image_upload')
        caption = request.POST['caption']

        new_post = Post.objects.create( image=image, caption=caption)
        new_post.save()

        #create pdf
        buffer = io.BytesIO()
        p = canvas.Canvas(buffer)


        p.drawString(100, 100, "Hello world.")
        p = request.FILES.get('post_pdf')
        p.drawText('caption')
        p.drawImage('image')
        p.showPage()
        p.save()
        buffer.seek(0)
        return FileResponse(buffer, as_attachment=True, filename='hello.pdf')


        return redirect('/')


    else:
        return redirect('/')


models.py

class Post(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4)

    image = models.ImageField(upload_to='post_images')
    caption = models.TextField(max_length=300)
    created_at = models.DateTimeField(auto_now_add=True)
    number_of_likes = models.IntegerField(default=0)
    number_of_dislikes = models.IntegerField(default=0)
    #interaction_count = models.IntegerField(default=0)

    #engagement_count = models.IntegerField(null=True)#number_of_dislikes + number_of_likes

    def __str__(self):
        return self.caption
2
  • you can use ReportLab to generate the PDFs Commented Jan 10, 2023 at 17:03
  • this is with ReportLab, but how do I put the values of my post in there? Commented Jan 10, 2023 at 17:15

2 Answers 2

1

To add the value of Post.caption into the pdf, use the value of new_post.caption, change this:

p.drawText('caption')

for this:

p.drawText(new_post.caption)

Same for other fields.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you. it works now with all the elements except the image. when i use p.drawImage(new_post.image) i get this error: AssertionError at /upload mode must be in (0,1,2,3,4,5,6,7). Do you now how to put the image in there as well?
Too add the image have a look at Proper way to add an image file inside a PDF document generated with Reportlab. Also please set the question as answered if it works to mark it as solved.
1

This is not as hard as it seems, so Let's see if you are successful in creating a pdf and now you have to store it in background instead of downloading.

file_name = request.FILES["file_name"]. #suppose file_name is file name
file_name = default_storage.save(rf"{basePath}/media/whatsapp/file_name.pdf", file_name) #{basePath}/media/whatsapp/ is the path name where we want it to be stored

3 Comments

Feel Free to ask any question to clarify the above code.
hi thank you for this answer. do i implement it in the models or in the upload function? Also, could you clarify how to implement it? I tried a bit around with this code snippet but couldn't get it to work.
so i numbered my posts now and would like to store the pdf in my media folder. it wasn't ´t working with your suggestion yet. so i did this: folder_path = f"media/post{num_post}.pdf" folder_name = os.path.basename(folder_path) but it is now saving in the project folder, not the media folder. do you know why?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.