I am getting this error " TypeError at /generate_pdf/10 expected str, bytes or os.PathLike object, not JpegImageFile"

Question:

I want to generate a pdf with content and uploaded images of each user uniquely in django but its throwing this error .This is the error message

This is the code to generate pdf

from django.shortcuts import render
from django.http import HttpResponse , HttpResponseServerError
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
from PIL import Image
from io import BytesIO
from django.core.files.base import ContentFile
import tempfile
import os
def generate_pdf(request, pid):
    # Get the user object based on the user_id
    user = Reports.objects.get(id=pid)

    # Create a new PDF object
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = f'attachment; filename="{user.Title}.pdf"'

    # Create the PDF using ReportLab
    pdf = canvas.Canvas(response, pagesize=(8.5*inch, 11*inch))

    # Open the uploaded image as an Image object
    try:
        image = Image.open(user.report_image)
    except Exception as e:
        print(f"Error opening image: {e}")
        return HttpResponseServerError("Error generating PDF")

    # Add the user's uploaded image to the PDF
    pdf.drawImage(image, 2*inch, 9.5*inch, width=3*inch, height=3*inch)

    # Add the user's content to the PDF
    pdf.setFont("Helvetica", 12)
    pdf.drawString(2*inch, 8*inch, user.Content)

    # Save the PDF and return it
    pdf.save()
    return response
Asked By: Rohith

||

Answers:

From the error message, it looks like the variable user.report_image is of type JpegImageFile, which is not compatible with the Image.open() method.

To solve this error, you can try converting the JpegImageFile object to a byte stream using the BytesIO() method from the io module, and then pass the byte stream to the Image.open() method.

Check this updated code:

from django.shortcuts import render
from django.http import HttpResponse , HttpResponseServerError
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
from PIL import Image
from io import BytesIO
from django.core.files.base import ContentFile
import tempfile
import os

def generate_pdf(request, pid):
    # Get the user object based on the user_id
    user = Reports.objects.get(id=pid)

    # Create a new PDF object
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = f'attachment; filename="{user.Title}.pdf"'

    # Create the PDF using ReportLab
    pdf = canvas.Canvas(response, pagesize=(8.5*inch, 11*inch))

    # Open the uploaded image as an Image object
    try:
        image_bytes = BytesIO()
        user.report_image.save(image_bytes, 'JPEG')
        image = Image.open(image_bytes)
    except Exception as e:
        print(f"Error opening image: {e}")
        return HttpResponseServerError("Error generating PDF")

    # Add the user's uploaded image to the PDF
    pdf.drawImage(image, 2*inch, 9.5*inch, width=3*inch, height=3*inch)

    # Add the user's content to the PDF
    pdf.setFont("Helvetica", 12)
    pdf.drawString(2*inch, 8*inch, user.Content)

    # Save the PDF and return it
    pdf.save()
    return response

Hopefully, this will resolve your problem.

Answered By: dostogircse171