Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 258
dola Avatar asked Dec 27 '22 11:12

dola


1 Answers

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
like image 174
Larry Lustig Avatar answered Jan 09 '23 21:01

Larry Lustig