2017-04-26 84 views
0

ここにイメージを中央に配置しようとしています。今、私はx軸に関して画像を中心に置くことができますが、実際の画像をその点に中心合わせすることはできません。代わりに、ページの中央でイメージを開始します。ReportLab:イメージをキャンバスにセンタリングする方法

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のみ。このロゴはキャンバスラインの真上にあり、中央に配置する必要があります。助けてください!

答えて

2

ここには2つのオプションがあります。あなたは完全な幅よりロゴが小さい場合

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 +いくつかのバッファで画像を始めることができ、あなたはこのような何かを行うことができますので、バウンディングボックスの中央に画像を固定するためのオプションがありdocumentation for drawImageで、さらにwidth = line_width - some_buffer*2

を設定します:

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

私はコードを追加/それを交換し、それは動作していません。私があなたの正しい編集で与えた完全なコードを書いても構わないと思いますか? –

+0

エラーが発生しましたか?画像は中央にないですか? –

+0

私は書いたものをテストしましたが、この正確なペーストはテストしませんでした:https://pastebin.com/ieLF7MGz –

関連する問題