Reportlab第x页由y编号的幻想和图像

时间:2009-07-06 15:05:24

标签: python reportlab

我一直在使用http://code.activestate.com/recipes/546511/处给出的reportlab NumberedCanvas。但是,当我尝试构建包含图像可流动图像的PDF时,图像不会显示,尽管留下足够的垂直空间以使图像适合。对此有什么解决方案吗?

1 个答案:

答案 0 :(得分:13)

请参阅my new, improved recipe,其中包括一个带图像的简单测试。以下是配方的摘录(省略了测试代码):

from reportlab.pdfgen import canvas
from reportlab.lib.units import mm

class NumberedCanvas(canvas.Canvas):
    def __init__(self, *args, **kwargs):
        canvas.Canvas.__init__(self, *args, **kwargs)
        self._saved_page_states = []

    def showPage(self):
        self._saved_page_states.append(dict(self.__dict__))
        self._startPage()

    def save(self):
        """add page info to each page (page x of y)"""
        num_pages = len(self._saved_page_states)
        for state in self._saved_page_states:
            self.__dict__.update(state)
            self.draw_page_number(num_pages)
            canvas.Canvas.showPage(self)
        canvas.Canvas.save(self)

    def draw_page_number(self, page_count):
        self.setFont("Helvetica", 7)
        self.drawRightString(200*mm, 20*mm,
            "Page %d of %d" % (self._pageNumber, page_count))