I got this following error message while creating a pdf using the python program "expected str, bytes or os.PathLike object, not JpegImageFile". It indicates that the drawing method expects a file path(string) ,byte objects or PathLike objects. But it receives directly the JpegImageFile object.
CODE:
import requests from reportlab.lib.pagesizes import letter from reportlab.pdfgen import canvas from PIL import Image from io import BytesIO def download_image_from_url(image_url): try: response = requests.get(image_url) if response.status_code == 200: image_data = BytesIO(response.content) return image_data else: print("Failed to download image from URL.") return None except Exception as e: print(f"Error downloading image: {e}") return None def create_pdf_from_image(image_url, pdf_filename): # Download image from URL image_data = download_image_from_url(image_url) if image_data: try: img = Image.open(image_data) pdf_path = f"{pdf_filename}.pdf" c = canvas.Canvas(pdf_path, pagesize=img.size) c.drawImage(img, 0, 0) c.save() print(f"PDF saved successfully: {pdf_path}") except Exception as e: print(f"Error creating PDF: {e}") image_url = 'https://tensix.com/wp-content/uploads/2023/11/P6-EPPM-Baseline-Update-Fig-4.jpg' pdf_filename = 'image_to_pdf' create_pdf_from_image(image_url, pdf_filename)
SOLUTION:
To resolve this problem download the image as temporary file and use its file path and draw it on PDF canvas.
import requests from reportlab.lib.pagesizes import letter from reportlab.pdfgen import canvas from PIL import Image from io import BytesIO import tempfile def download_image_from_url(image_url): try: response = requests.get(image_url) if response.status_code == 200: image_data = BytesIO(response.content) return image_data else: print("Failed to download image from URL.") return None except Exception as e: print(f"Error downloading image: {e}") return None def create_pdf_from_image(image_url, pdf_filename): # Download image from URL image_data = download_image_from_url(image_url) if image_data: try: img = Image.open(image_data) temp_img_file= tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') temp_img_path= temp_img_file.name img.save(temp_img_path) temp_img_file.close() pdf_path = f"{pdf_filename}.pdf" c = canvas.Canvas(pdf_path, pagesize=img.size) c.drawImage(temp_img_path, 0, 0) c.save() print(f"PDF saved successfully: {pdf_path}") except Exception as e: print(f"Error creating PDF: {e}") image_url = 'https://tensix.com/wp-content/uploads/2023/11/P6-EPPM-Baseline-Update-Fig-4.jpg' pdf_filename = 'image_to_pdf' create_pdf_from_image(image_url, pdf_filename)
VIDEO GUIDE:
Post your comments / questions
Recent Article
- How to restrict access to the page Access only for logged user in Django
- Migration admin.0001_initial is applied before its dependency admin.0001_initial on database default
- Add or change a related_name argument to the definition for 'auth.User.groups' or 'DriverUser.groups'. -Django ERROR
- Addition of two numbers in django python
- The request was aborted: Could not create SSL/TLS secure channel -Error in Asp.net
- FieldError: Cannot resolve keyword 'id' into field in Django project
- How to hide the ID field from the Django admin?
- It is impossible to add a non nullable field without specifying a default. Django error
Related Article