4

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

Community
  • 1
  • 1
dola
  • 201
  • 4
  • 9

1 Answers1

12

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
Larry Lustig
  • 49,320
  • 14
  • 110
  • 160
  • 2
    If you are using `SimpleDocTemplate` to build your document, you can pass the page setup function to the `build` method: `doc.build(story, onFirstPage=AllPageSetup, onLaterPages=AllPageSetup)` – tobygriffin Feb 17 '15 at 02:26
  • how to add a canvas.drawimage with a filename and fits to the full pagesize?? – DeadDjangoDjoker Jun 30 '15 at 17:12