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 hide the ID field from the Django admin?
- It is impossible to add a non nullable field without specifying a default. Django error
- ImportError: cannot import name 'url' from 'django.conf.urls' - Django Error
- How to Enable Virtualization in BIOS Security Settings in Intel Processors For Android Studio?
- Dependency 'androidx.activity:activity:1.8.0' requires libraries and applications that depend on it.
- AttributeError: 'NoneType' object has no attribute 'get_text' - Python
- ModuleNotFoundError: No module named 'openpyxl' - Python
- How to get thumbnail from vimeo video URL in Python?
Related Article