1

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.

Community
  • 1
  • 1
Ngure Nyaga
  • 2,989
  • 1
  • 20
  • 30
  • You're not getting any errors/warnings at all when you execute this function? – Pedro Romano Oct 22 '12 at 12:04
  • When this function is executed [ passed into a build(...) call on a SimpleDocTemplate instance ] it does not give any errors. All the lines and strings render correctly, including the "bottom line" that is defined in the last statement. – Ngure Nyaga Oct 22 '12 at 12:06
  • ReportLab uses [PIL](http://www.pythonware.com/products/pil/) for reading bitmaps. Test if your PIL package can read the JPEG correctly. – Pedro Romano Oct 22 '12 at 12:08
  • Thanks for the suggestion. The documentation suggests that JPEG images are read by reportlab natively, with or without PIL. Even so, I have confirmed that the PIL can read this JPG correctly. I have also found that using "drawInlineImage" instead of "drawImage" works [ with nothing else changed in the code ]. I will edit the question with the new information, but keep it open since I still do not understand why drawImage is failing in this case ( but working in simple console tests ) – Ngure Nyaga Oct 22 '12 at 12:10
  • You are right about ReportLab native JPEG support. Good you found a workaround. – Pedro Romano Oct 22 '12 at 12:13

0 Answers0