# Template definitions for different label layouts from PIL import Image, ImageDraw, ImageFont from datetime import date def vet_label_template(variables, width_pixels=991, height_pixels=306, filename=None): """Default vet label template""" image = Image.new('RGB', (width_pixels, height_pixels), 'white') draw = ImageDraw.Draw(image) try: font_bold = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf', 36) font_normal = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 32) font_small = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 28) font_footer = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Oblique.ttf', 22) except: font_bold = ImageFont.load_default() font_normal = ImageFont.load_default() font_small = ImageFont.load_default() font_footer = ImageFont.load_default() margin = 10 top = margin left_col = margin x1 = 200 x2 = 500 x3 = 750 line_height = 50 # Practice name (top left) draw.text((left_col, top), variables.get('practice_name', ''), fill='black', font=font_bold) # Date (top right) today = date.today().strftime("%d %b %Y") bbox = draw.textbbox((0, 0), today, font=font_small) date_width = bbox[2] - bbox[0] draw.text((width_pixels - margin - date_width, top), today, fill='black', font=font_small) # Three columns of data y = top + line_height + 10 # Column 1 draw.text((left_col, y), "Animal:", fill='black', font=font_normal) draw.text((left_col, y + line_height), "Drug:", fill='black', font=font_normal) draw.text((left_col, y + 2 * line_height), "Dose:", fill='black', font=font_normal) # Column 2 draw.text((x1, y), variables.get('animal_name', ''), fill='black', font=font_normal) draw.text((x1, y + line_height), variables.get('drug_name', ''), fill='black', font=font_normal) draw.text((x1, y + 2 * line_height), variables.get('dosage', ''), fill='black', font=font_normal) # Column 3 draw.text((x2, y), "Qty:", fill='black', font=font_normal) draw.text((x2, y + line_height), "Vet:", fill='black', font=font_normal) draw.text((x3, y), variables.get('quantity', ''), fill='black', font=font_normal) draw.text((x3, y + line_height), variables.get('vet_initials', ''), fill='black', font=font_normal) # Footer at bottom footer_text = "For animal treatment only" bbox = draw.textbbox((0, 0), footer_text, font=font_footer) text_width = bbox[2] - bbox[0] footer_x = (width_pixels - text_width) / 2 footer_y = height_pixels - margin - 20 draw.text((footer_x, footer_y), footer_text, fill='black', font=font_footer) if filename: image.save(filename) return image # Template registry TEMPLATES = { 'vet_label': vet_label_template, # Add more templates here, e.g., 'vet_label_small': vet_label_small_template }