简体   繁体   中英

reportlab: add background image by using platypus

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM