from reportlab.pdfgen import canvas from reportlab.lib.pagesizes import letter from reportlab.lib.units import inch from reportlab.pdfbase.pdfmetrics import stringWidth OUT_PATH = r"C:\Users\jhodgkin\Pictures\Dearest Mother I Love You - Initials.pdf" LINES = [ "Gentle words I hear you say.", "Your kind hands help me each day.", "You're my mother kind and true;", "Dearest mother, I love you.", ] W, H = letter MARGIN = 0.5 * inch FONT = "Helvetica-Bold" def first_letters(line): return " ".join(word[0].upper() for word in line.split()) def fit_font_size(c, text, max_width, start=200): size = start while size > 1: if stringWidth(text, FONT, size) <= max_width: return size size -= 1 return size def make_pdf(): c = canvas.Canvas(OUT_PATH, pagesize=letter) initials = [first_letters(line) for line in LINES] max_w = W - 2 * MARGIN # Find a single font size that fits all lines size = min(fit_font_size(c, line, max_w) for line in initials) # Distribute lines evenly across the page height usable_h = H - 2 * MARGIN slot = usable_h / len(initials) for i, text in enumerate(initials): # Center vertically within each slot y = H - MARGIN - (i + 0.65) * slot c.setFont(FONT, size) c.drawCentredString(W / 2, y, text) c.save() print(f"Saved: {OUT_PATH}") print(f"Font size: {size}pt") for line, init in zip(LINES, initials): print(f" {line!r} -> {init!r}") make_pdf()