Background
I am writing some code to set up a shared document "template" [ common header and footer ] in ReportLab using a SimpleDocTemplate
( reportlab.platypus.SimpleDocTemplate
). The code snippet below is a function that is to be passed into the SimpleDocTemplate
build(...)
method as the value of either the onFirstPage
or onLaterPages
parameter.
def setup_header_and_footer(canvas, doc):
"""
...edited out...
"""
canvas.line(0 * mm, 174 * mm, 297 * mm, 174 * mm)
logo_filename = settings.STATIC_ROOT + os.sep + "images/huqas_logo.jpg"
canvas.drawImage(logo_filename, 20 * mm, 45 * mm)
canvas.drawRightString(287 * mm, 200 * mm, "<edited out>")
canvas.drawString(20 * mm, 15 * mm, "Generated on %s" % datetime.now().strftime("%A %d %B %Y %I:%M:%S %p"))
canvas.line(0 * mm, 20 * mm, 297 * mm, 20 * mm)
Further Information
- The page / canvas has been set up with A4 landscape dimensions
- I have confirmed that logo_filename is a valid path, and that the image exists at that location in the filesystem
- Reportlab does not raise any exception
The Problem
- My lines and strings render OK, but the image is not visible in the resulting PDFs
I have looked at this question but I still do not understand why the canvas would "behave" when calling eg canvas.line(0 * mm, 174 * mm, 297 * mm, 174 * mm)
but fail when calling canvas.drawImage("file name", 20 * mm, 45 * mm)
. I have also scoured the documentation to no avail. What am I missing?
Update
Changing from canvas.drawImage(logo_filename, 20 * mm, 45 * mm)
to canvas.drawInlineImage(logo_filename, 20 * mm, 45 * mm)
appears to "fix" the issue [ without changing any other line of code ]. I am still puzzled as to why drawImage
did not work.