我的字体在哪里重置?

时间:2014-02-12 00:43:54

标签: python pdf-generation reportlab

我正在写一个小程序来制作一堆BINGO卡。

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()

我还有一些工作要做(“FREE”应该居中!我需要画线)但这基本上有效。我执行r = set_ranges()c = set_canvas(),然后for i in range (1,25): print_card(r,c)创建一个包含基本卡片的PDF文件。

但在第一页之后,字体不再是粗体。它会在哪里重置?

2 个答案:

答案 0 :(得分:0)

您使用的是什么版本的ReportLab?版本1.1.0(2012-12-18)有一个错误,有时会在渲染之前重置字体...

答案 1 :(得分:0)

来自reportlab参考:

  

def save(self):保存并关闭文件中的PDF文档。如果有   是当前数据,ShowPage是自动执行的。在这之后   操作画布不得再使用。

所以你的代码工作很奇怪。您可以生成所有表格,然后再调用canvas.save()