#!/usr/bin/env python # Generate a PDF page of business cards. # Adam Sampson from reportlab.lib.pagesizes import A4 from reportlab.lib.units import mm from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.ttfonts import TTFont from reportlab.pdfgen import canvas pdfmetrics.registerFont(TTFont("GentiumBasic", "/opt/share/fonts/gentiumbasic/GenBkBasR.ttf")) pdfmetrics.registerFont(TTFont("GentiumBasic-Bold", "/opt/share/fonts/gentiumbasic/GenBkBasB.ttf")) # UK standard business cards are ISO 8710 ID-1. card_size = (85.60 * mm, 53.98 * mm) # A8 is an alternative. #card_size = (74 * mm, 52 * mm) page_size = A4 tick_len = 3 * mm def draw_card(c): """Draw a single card at (0, 0).""" w, h = card_size c.setFont("GentiumBasic-Bold", 20) c.drawCentredString(w / 2, h * .75, "Adam Sampson") c.setFont("GentiumBasic", 16) c.drawCentredString(w / 2, h * .45, "ats@offog.org") c.drawCentredString(w / 2, h * .30, "http://offog.org/") c.drawCentredString(w / 2, h * .15, "+44 (0)7930 250969") def main(): c = canvas.Canvas("businesscard.pdf", pagesize = page_size) # How many cards can we fit on the page? w_cards = int((page_size[0] - (2 * tick_len)) / card_size[0]) h_cards = int((page_size[1] - (2 * tick_len)) / card_size[1]) margin = ((page_size[0] - (w_cards * card_size[0])) / 2, (page_size[1] - (h_cards * card_size[1])) / 2) for x in range(w_cards): for y in range(h_cards): c.saveState() c.translate(margin[0] + (x * card_size[0]), margin[1] + (y * card_size[1])) draw_card(c) c.restoreState() def draw_tick(x, y): c.line(x - tick_len, y, x + tick_len, y) c.line(x, y - tick_len, x, y + tick_len) for x in range(w_cards + 1): for y in range(h_cards + 1): draw_tick(margin[0] + (x * card_size[0]), margin[1] + (y * card_size[1])) c.showPage() c.save() if __name__ == "__main__": main()