Reporlab使用类PDFImage更改hAline

问题描述 投票:0回答:1

你好all我是python的初学者,所以我还不了解所有,但这里是我的问题的背景。

我正在使用reportlab制作一份自动报告。在这里我使用PDF图像。为了使它兼容我使用这个classe(有一个例子)

enter code here 
class PdfImage(Flowable):

def __init__(self, filename_or_object, width=None, height=None, kind='direct'):
    from reportlab.lib.units import inch
    # If using StringIO buffer, set pointer to begining
    if hasattr(filename_or_object, 'read'):
        filename_or_object.seek(0)
    page = PdfReader(filename_or_object, decompress=False).pages[0]
    self.xobj = pagexobj(page)
    self.imageWidth = width
    self.imageHeight = height
    x1, y1, x2, y2 = self.xobj.BBox

    self._w, self._h = x2 - x1, y2 - y1
    if not self.imageWidth:
        self.imageWidth = self._w
    if not self.imageHeight:
        self.imageHeight = self._h
    self.__ratio = float(self.imageWidth)/self.imageHeight
    if kind in ['direct','absolute'] or width==None or height==None:
        self.drawWidth = width or self.imageWidth
        self.drawHeight = height or self.imageHeight
    elif kind in ['bound','proportional']:
        factor = min(float(width)/self._w,float(height)/self._h)
        self.drawWidth = self._w*factor
        self.drawHeight = self._h*factor

def wrap(self, aW, aH):
    return self.drawWidth, self.drawHeight

def drawOn(self, canv, x, y, _sW=0):
    if _sW > 0 and hasattr(self, 'hAlign'):
        a = self.hAlign
        if a in ('CENTER', 'CENTRE', TA_CENTER):
            x += 0.5*_sW
        elif a in ('RIGHT', TA_RIGHT):
            x += _sW
        elif a not in ('LEFT', TA_LEFT):
            raise ValueError("Bad hAlign value " + str(a))

    xobj = self.xobj
    xobj_name = makerl(canv._doc, xobj)

    xscale = self.drawWidth/self._w
    yscale = self.drawHeight/self._h

    x -= xobj.BBox[0] * xscale
    y -= xobj.BBox[1] * yscale

    canv.saveState()
    canv.translate(x, y)
    canv.scale(xscale, yscale)
    canv.doForm(xobj_name)
    canv.restoreState()

Title = "Hello world"
pageinfo = "platypus example"
def myFirstPage(canvas, doc):
    canvas.saveState()
    canvas.setFont('Times-Bold',16)
    canvas.drawCentredString(PAGE_WIDTH/2.0, PAGE_HEIGHT-108, Title)
    canvas.setFont('Times-Roman',9)
    canvas.drawString(inch, 0.75 * inch, "First Page / %s" % pageinfo)
canvas.restoreState()


def myLaterPages(canvas, doc):
    canvas.saveState()
    canvas.setFont('Times-Roman',9)
    canvas.drawString(inch, 0.75 * inch, "Page %d %s" % (doc.page,    pageinfo))
    canvas.restoreState()

def go():
    fig = plt.figure(figsize=(4, 3))
    plt.plot([1,2,3,4])
    plt.ylabel('some numbers')
    imgdata = io.BytesIO()
    fig.savefig(imgdata,format='PDF')
    doc = SimpleDocTemplate("document.pdf")
    Story = [Spacer(1,2*inch)]
    style = styles["Normal"]

    for i in range(5):
        bogustext = ("This is Paragraph number %s.  " % i) *20
        p = Paragraph(bogustext, style)
        Story.append(p)
        Story.append(Spacer(1,0.2*inch))
        pi = PdfImage(imgdata)

        Story.append(pi)
        Story.append(Spacer(1,0.2*inch))
    doc.build(Story, onFirstPage=myFirstPage, onLaterPages=myLaterPages)
go()

我的问题非常简单,但我无法正确完成,如何更改hAlin值?当你看到它可能的代码时,它似乎。

谢谢你们!非常简单,但很烦人

python python-3.x image pdf reportlab
1个回答
0
投票

我刚看到你的问题,我猜你已经解决了你的问题。否则,你应该在PdfImage类的init函数中添加一个链接到hAlign的参数(我不知道这是否是最好的解决方案,但它对我有用),它看起来应该是这样的:

class PdfImage(Flowable):
    def __init__(self, img_data, width=200, height=200, align='LEFT'):
        self.img_width = width
        self.img_height = height
        self.img_data = img_data
        self.hAlign = align

(原始代码的所有其他行保持不变)。当您使用PdfImage时,您只需将align设置为Reportlab的hAlign,它应该可以工作。希望这个答案有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.