Search code examples
pythonpdf-generationreportlab

Where is my font getting reset?


I'm writing a little program to produce a bunch of BINGO cards.

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import landscape, letter
from reportlab.lib.units import inch
import random


def set_ranges():
    r = {
        "b": [1, 15, 0],
        "i": [16, 30, 0],
        "n": [31, 45, 0],
        "g": [46, 60, 0],
        "o": [61, 75, 0]
    }
    return r

def set_canvas():
    c = canvas.Canvas("bingo.pdf")
    c.setPageRotation(90)
    c.setFont('Helvetica-Bold', 14)
    print type(c)
    return c

def print_card(ranges, canvas):
    # Set a page gutter
    gutter = 1 * inch
    x = gutter
    y = 8.5 * inch - 2 * gutter
    # First draw the letters themselves
    for letter in "bingo":
        canvas.drawString(x, y, letter)
        # Print X and Y to troubleshoot
        # print('%s, %d, %d' % (letter, x, y))
        # Add the X value for each letter to the dictionary
        ranges[letter][2] = x
        x = x + 1 * inch
    y_reset = y - 1 * inch
    # Then pull the numbers for each square
    for letter in "bingo":
        row = random.sample(range(ranges[letter][0], ranges[letter][1] + 1), 5)
        if letter == "n":
            row[2] = "FREE"            
        x = ranges[letter][2]
        y = y_reset
        for col in row:
            # Print X and Y to troubleshoot
            # print('%d, %d, %d' % (col, x, y))
            canvas.drawString(x, y, str(col))
            y = y - 1 * inch
    canvas.save()

I still have some work to do ("FREE" should be centered! and I need to draw lines) but this basically works. I do r = set_ranges() and c = set_canvas() and then for i in range (1,25): print_card(r,c) to create a PDF full of basic cards.

But after the first page, the font ceases to be bold. Where would it be getting reset?


Solution

  • What version of ReportLab are you using? Version 1.1.0 (2012-12-18) has a bug that resets fonts before rendering sometimes...