app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
from PIL import Image, ImageDraw, ImageFont def create_simple_form(output_pdf_path, profile_picture_path): # Page dimensions (A4 size in pixels for a resolution of 96 DPI) page_width, page_height = 794, 1123 # Create a blank image with white background page = Image.new("RGB", (page_width, page_height), "white") draw = ImageDraw.Draw(page) # Load a font (adjust font size based on your system's available fonts) try: font_title = ImageFont.truetype("arial.ttf", 24) font_text = ImageFont.truetype("arial.ttf", 16) except IOError: font_title = ImageFont.load_default() font_text = ImageFont.load_default() # Title title_text = "Simple Registration Form" title_x = (page_width - draw.textsize(title_text, font=font_title)[0]) / 2 draw.text((title_x, 50), title_text, fill="black", font=font_title) # Form labels and data form_labels = [ ("First Name:", "Gautam", (100, 150)), ("Last Name:", "Sharma", (100, 200)), ("Email:", "gautam@example.com", (100, 250)), ("Age:", "27", (100, 300)) ] for label, value, position in form_labels: draw.text(position, label, fill="black", font=font_text) draw.text((position[0] + 150, position[1]), value, fill="black", font=font_text) # Draw a rectangle around the text (to simulate a text field) field_x1, field_y1 = position[0] + 145, position[1] - 5 field_x2, field_y2 = position[0] + 300, position[1] + 20 draw.rectangle([field_x1, field_y1, field_x2, field_y2], outline="black") # Profile picture section draw.text((100, 350), "Profile Picture:", fill="black", font=font_text) # Position and size for the profile picture pic_x1, pic_y1 = 250, 370 pic_x2, pic_y2 = 450, 570 try: profile_pic = Image.open(profile_picture_path) profile_pic = profile_pic.resize((pic_x2 - pic_x1, pic_y2 - pic_y1)) page.paste(profile_pic, (pic_x1, pic_y1)) except IOError: print("Profile picture not found or unable to load.") draw.rectangle([pic_x1, pic_y1, pic_x2, pic_y2], outline="black") # Placeholder rectangle # Save the form as a PDF page.save(output_pdf_path, "PDF") print(f"PDF saved as {output_pdf_path}") # Usage example output_pdf = "simple_registration_form.pdf" profile_picture_path = "profile.jpg" # Update with the correct path to your profile picture create_simple_form(output_pdf, profile_picture_path) |