reportlab: add background image by using platypus

放肆的年华 提交于 2020-01-01 14:51:29

问题


this is a bit related to this post

I am trying to place an image on the background, and I want to be able to write text over it. using canvas.drawImage helps, but that's too low level approach for me.
My program uses platypus, but canvas.drawImage is part of different library. I've been able to insert images with platypus.Image, but couldn't figure out how to make it as background.
Any advice would be great,

Thanks D


回答1:


When you create a page template in Platypus you have the ability to pass a function via the named argument onPage. In that function you can place all your basic page formatting (headers, footers, watermark, background image).

Here's an example:

def AllPageSetup(canvas, doc):

    canvas.saveState()

    #header
    canvas.drawString(0.5 * inch, 8 * inch, doc.fund.name)
    canvas.drawRightString(10.5 * inch, 8 * inch, doc.report_info)

    #footers
    canvas.drawString(0.5 * inch, 0.5 * inch, 'Larry Co.')
    canvas.drawRightString(10.5 * inch, 0.5 * inch, 'Page %d' % (doc.page))

    canvas.setFont("Helvetica", 240)
    canvas.setStrokeGray(0.90)
    canvas.setFillGray(0.90)
    canvas.drawCentredString(5.5 * inch, 3.25 * inch, doc.watermark)

    canvas.restoreState()

doc = BaseDocTemplate(file_name)

doc.fund = fund # stores my fund object into the document for reference
doc.report_info = "%s %s" % (fund.current_report.date.isoformat(), version)
doc.watermark = 'DRAFT'

page_template = PageTemplate(id="fund_notes", onPage=AllPageSetup, pagesize=page_size)

#Now, every page will have headers, footers, and a watermark


来源:https://stackoverflow.com/questions/8185438/reportlab-add-background-image-by-using-platypus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!