Reportlab Canvas Image Not Showing

丶灬走出姿态 提交于 2019-12-11 09:07:38

问题


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.

来源:https://stackoverflow.com/questions/13010930/reportlab-canvas-image-not-showing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!