[reportlab-users] is Platypus just slow

David S. davidschein at alumni.tufts.edu
Mon Feb 6 14:57:21 EST 2006


I have the class shown below, but it takes hours to run over an even moderately
sited report (~5000 records) with toPDF().  Am I doing something obviously wrong?

Thanks so much,
David S.

class PDF(object):
  """report object"""
  def __init__(self, rows, header=[], title="NCAA Report"):
    """contructor

    rows: sequence of sequences
    header: optional sequence of sequences represting repeating header (ie, for
column names)
    title: optional title

    """
    self.rows = rows
    self.header = header
    self.title = title
    self.pageheader = ' '.join([self.title, date.today().strftime("%b %d, %Y")])

  def _standardFrame(self, canvas, doc):
    canvas.setTitle(self.title)
    canvas.saveState()
    #canvas.setPageSize(landscape(letter))
    canvas.setFont('Times-Italic', 12)
    canvas.drawRightString(land_width-1*inch, land_height-0.5*inch, self.pageheader)
    canvas.setFont('Times-Roman',12)
    canvas.drawString(4*inch, 0.5*inch, "Page %d" % canvas.getPageNumber())
    canvas.restoreState()
    canvas.setFont('Times-Roman',11)
  
  def toPDF(self):
    """generates a PDF file
    """
    #styleSheet = getSampleStyleSheet()
    # reportlab expects to see XML-compliant
    # data; need to escape ampersands &c.
    #escLines = [cgi.escape(l) for l in self.rows] make the client do this
    if self.header:
      content = self.header + self.rows
    else:
      content = self.rows[:]
    story = []
    repeatRows = len(self.header)
    tableStyle = [ ('FONT', (0, 0), (-1,0), 'Times-Bold', 11),
            ('FONT', (0, 1), (-1,-1), 'Times-Roman', 11)]
    t = Table(content, repeatRows=repeatRows, style=tableStyle) #, (56, 56))
    story.append(t)
    buffer = StringIO()
    doc = SimpleDocTemplate(buffer, pagesize=landscape(letter),
leftMargin=.5*inch, rightMargin=.5*inch, title=self.title)
    doc.build(story, onFirstPage=self._standardFrame,
onLaterPages=self._standardFrame)
    buffer.flush()
    pdf = buffer.getvalue()
    buffer.close()
    return pdf




More information about the reportlab-users mailing list