ReportLab:如何在画布上居中一张图片

4

在这里尝试居中一张图片。现在,我能够将图片相对于x轴居中,但我无法将实际图片居中在该点上。相反,它会从页面的中心开始显示图片。

from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import inch
from reportlab.lib.units import mm
from reportlab.platypus import Image
from reportlab.lib.enums import TA_JUSTIFY
from reportlab.lib.utils import ImageReader
from reportlab.pdfgen import canvas

Width, Height = letter
styles = getSampleStyleSheet()
Title = "Comparison Index"
pageinfo = "platypus example"
BTRLogo = 'BTALogo.png'
PartnerLogo = 'PartnerLogo.png'
ClientLogo = 'ClientLogo.png'
Graph1 = 'PlanOffered.jpg'



# Define the fixed features of the first page of the document
def myFirstPage(canvas, doc):
    canvas.saveState()

    canvas.setFillColorCMYK(0.68, 0.44, 0, 0.41)
    canvas.setFontSize(22)
    canvas.setFont('Helvetica-Bold', 36)
    canvas.drawString(40, 670, 'Health & Welfare')

    canvas.setFont('Helvetica-Bold', 24)
    canvas.drawString(40, 625, 'Benchmark Comparison Report')

    canvas.setFont('Helvetica', 16)
    canvas.drawString(40, 550, 'Prepared for:')
    canvas.drawString(40, 400, 'Prepared on:') #INSERY DYNAMIC DATE****

    canvas.drawImage(BTRLogo,480,18,width=100,height=66.62,mask='auto') 
    #Logo is measured and good to go

    canvas.drawImage(PartnerLogo, 10, Height/5, width=Width/1.2, 
    preserveAspectRatio=True, mask='auto') #MAKE SURE IMAGE IS DYNAMIC AND HAS MAX SETS

    canvas.setStrokeColorCMYK(0.68,0.44,0,0.41)
    canvas.setLineWidth(7)
    canvas.line(40,112,570,112)



# Since we want pages after the first to look different from the first we define an alternate layout for
# the fixed features of the other pages.
# Note that the two functions use the pdfgen level canvas operations to paint the annotations for the pages.
def myLaterPages(canvas, doc):
    canvas.saveState()
    canvas.setFont('Times-Roman', 23)
    page_num = canvas.getPageNumber()
    text = "Page #%s" % page_num
    canvas.drawRightString(200 * mm, 20 * mm, text)
    canvas.restoreState()

def myThirdPages(canvas, doc):
    canvas.saveState()
    canvas.setFont('Times-Roman', 9)
    canvas.drawString(inch, 0.75 * inch, "Page %d %s")
    canvas.restoreState()

# Create a story and build the document
def createMultiPage():

    doc = SimpleDocTemplate("Comparison Index.pdf", pagesize=letter, rightMargin=72, leftMargin=72, topMargin=72, bottomMargin=18)
    style = styles["Normal"]
    styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY))

    Story = [Spacer(1, 2 * inch)]

    doc.build(Story, onFirstPage=myFirstPage, onLaterPages=myLaterPages)

if __name__ == "__main__":
    createMultiPage()

所以,最终我需要确保我的图像在x轴上居中。具体来说,只有PartnerLogo需要这样做。此标志需要位于画布线的正上方并居中。请帮忙!

1个回答

4

您有几个选项。如果您只想将图像居中于您的行上方,而该行的位置是固定的,则可以根据该行的位置绘制图像:

line_x_start = 40
line_width = 530
line_y = 112

canvas.setStrokeColorCMYK(0.68,0.44,0,0.41)
canvas.setLineWidth(7)
canvas.line(line_x_start, line_y,
            line_x_start+line_width, line_y)

canvas.drawImage(PartnerLogo, line_x_start, line_y, width=line_width, 
                 preserveAspectRatio=True, mask='auto') 

如果你想让标志比线的完整宽度更小,可以从line_x_start +一些缓冲开始图像,并设置width=line_width-某些缓冲*2 此外,在 drawImage 的文档中,有一个选项可以将图像锚定在边界框的中心,因此你可以这样做:
canvas.drawImage(PartnerLogo, line_x_start, line_y, width=line_width, 
                 preserveAspectRatio=True, mask='auto', anchor='c') 

我添加了代码/更改了它,但它不起作用。你愿意用你的正确修改写出我给出的完整代码吗? - Julian Olivares
你看到错误了吗?图片没有居中吗? - Kyle Heuton
NameError: 全局名称 'line_width' 未定义 handle_pageBegin args=() - Julian Olivares
啊,你说得对。它以前被称为 line_x_width。将其替换为 line_width = 530 应该可以解决这个问题。 - Kyle Heuton
太棒了,谢谢!现在我遇到了故事无法运行的问题。我只学了3天的Python。真的需要帮助来完成客户的工作。我们的第三方制片人已经退出了。你有没有可能帮我让myLaterPages和myThirdPages正常运行?或者你能否加入Uber会议并教我15-30分钟? - Julian Olivares
非常抱歉,虽然我曾经处于类似的情况(我的第一个Python项目是使用ReportLab来满足紧急的外部截止日期),但那已经是多年前的事了,而且我对ReportLab的了解也有所减少。我仍然知道在文档中查找简单事物的位置,但我现在没有时间帮助处理更复杂的请求。祝你的项目好运! - Kyle Heuton

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接