from reportlab.lib.pagesizes import letter from reportlab.pdfgen import canvas from PIL import Image
def create_pdf_with_images(pdf_filename, image_paths): # Create a canvas object to generate the PDF c = canvas.Canvas(pdf_filename, pagesize=letter)
# Add a title to the document
c.setFont("Helvetica-Bold", 16)
c.drawString(100, 750, "Basic Elements of Computing")
# Adding an introduction or explanation
c.setFont("Helvetica", 12)
c.drawString(100, 730, "This document explains the basic elements of computing with illustrations.")
# Define the starting position for images and text
y_position = 700
for image_path in image_paths:
# Add a brief description
c.setFont("Helvetica", 10)
c.drawString(100, y_position - 20, f"Image: {image_path}")
# Add the image (resize it if necessary)
img = Image.open(image_path)
img_width, img_height = img.size
aspect_ratio = img_height / float(img_width)
img_width = 200 # Width of the image in the PDF
img_height = img_width * aspect_ratio # Maintain the aspect ratio
# Draw the image onto the canvas
c.drawImage(image_path, 100, y_position - 150, width=img_width, height=img_height)
# Update y_position for the next image
y_position -= img_height + 100
# Save the PDF
c.save()
image_paths = ['image1.jpg', 'image2.png', 'image3.jpg'] # Provide the paths of your images create_pdf_with_images("Computing_Elements.pdf", image_paths)