'Is there a way of using FPDF to generate a PDF file from forms in Django?

I am trying to generate a PDF file and give it a custom name using Django 4.0.2 I have 2 inputs, one for the name and one for the photos, I want to be able to generate the PDF without saving it in my database but return it back to the user. I am using no models for this, plain html and python. I have the inputs:

<input
                    type="text"
                    id="inputPassword6"
                    class="form-control"
                    aria-describedby="passwordHelpInline"
                    name="name"
                />
<input
                    class="form-control"
                    type="file"
                    id="formFileMultiple"
                    multiple
                    accept=".png, .jpeg, .jpg"
                    name="photos"
                />
        <div class="d-grid gap-2 mt-3">
            <button class="btn btn-success" type="submit">Save PDF</button>
        </div>

And I am trying to merge and return the PDF file like this:

    if request.method == "POST" or "FILES":
        name = request.POST["name"]
        photos = request.FILES["photos"]

        # Convert photos to PDF
        pdf = FPDF()
        # imagelist is the list with all image filenames
        for image in photos:
            pdf.add_page()
            pdf.image(image, 0,0,210,297)
        pdf.output(f"{name} AIA 114.pdf", "F")

Current error:

Exception Type: TypeError
Exception Value:    
argument should be integer or bytes-like object, not 'str'

It looks like FPDF can not look into the image I provided. I tried to decode the image, but I hit another error:

'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

Can someone help me solve this problem or propose another way of doing it?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source