ReportLab:将文本与框架内的 KeepInFrame 对齐不起作用

ReportLab: align text with KeepInFrame inside Frame not working

我正在尝试使用 ReportLab 在 Frame 中水平和垂直对齐文本。

问题是,即使为 KeepInFrame 函数显式定义参数 hAlign='CENTER'vAlign='BOTTOM',也不会更改默认的左水平对齐和顶部垂直对齐顶部。

我正在使用最新版本的 ReportLab (reportlab==3.6.9)。

这是一个代码示例:

# Import packages
import re
from reportlab.lib.enums import TA_CENTER
from reportlab.lib.pagesizes import A4
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.units import cm
from reportlab.pdfgen.canvas import Canvas
from reportlab.platypus import Paragraph, Frame, KeepInFrame

# Create document
doc = Canvas(filename='test.pdf', pagesize=A4, bottomup=1, pdfVersion=(1,4))

# Text
text = ("""Here is a simple text that should be hAlign='CENTER' and vAlign='BOTTOM'""")
text = re.sub(r'\n', '<br/>', text)
text = Paragraph(text, ParagraphStyle(name='', fontName='Helvetica', fontSize=11, textColor='black', alignment=TA_CENTER), encoding='utf8')
text = KeepInFrame(
    maxWidth=0,
    maxHeight=0,
    content=[text],
    mode='shrink',
    hAlign='CENTER',
    vAlign='BOTTOM',
    fakeWidth=False,
)

# Create ReportLab Frame object
frame = Frame(
    x1=2.5*cm,
    y1=20*cm,
    width=9.0*cm,
    height=1.5*cm,
    showBoundary=1
)

frame.addFromList([text], doc)

# Save document
doc.save()

输出:

有谁知道如何解决这个问题?提前致谢。

更新:我能够通过将 ParagraphTable 函数(而不是 Frame 结合)来解决这个问题。

根据ReportLab's documentation (page 68)

All flowables have an hAlign property: ('LEFT', 'RIGHT', 'CENTER' or 'CENTRE'). For paragraphs, which fill the full width of the frame, this has no effect. For tables, images or other objects which are less than the width of the frame, this determines their horizontal placement.

这是所需输出的工作示例:

# Import packages
import re
from reportlab.lib.enums import TA_CENTER
from reportlab.lib.pagesizes import A4
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.units import cm
from reportlab.pdfgen.canvas import Canvas
from reportlab.platypus import Paragraph, KeepInFrame, Table, TableStyle

# Create document
document = Canvas(filename='test.pdf', pagesize=A4, bottomup=1, pdfVersion=(1,4))

# Text
text = ("""Here is a simple text that should be hAlign='CENTER' and vAlign='BOTTOM'""")
text = re.sub(r'\n', '<br/>', text)
text = Paragraph(text, ParagraphStyle(name='', fontName='Helvetica', fontSize=11, textColor='black', alignment=TA_CENTER), encoding='utf8')
text = KeepInFrame(
    maxWidth=0,
    maxHeight=0,
    content=[text],
    mode='shrink',
    #hAlign='CENTER',
    #vAlign='BOTTOM',
    #fakeWidth=False
)

# Create ReportLab Table object
table = Table(
        data=[[text]],
        colWidths=9.0*cm,
        rowHeights=1.5*cm,
        spaceBefore=0,
        spaceAfter=0,
        #hAlign='CENTER',
        #vAlign='BOTTOM',
    )

# Set table style
table.setStyle(TableStyle([
        ('LEFTPADDING', (0, 0), (-1, -1), 0),
        ('RIGHTPADDING', (0, 0), (-1, -1), 0),
        ('TOPPADDING', (0, 0), (-1, -1), 0),
        ('BOTTOMPADDING', (0, 0), (-1, -1), 0),
        ('VALIGN', (0, 0), (-1, -1), 'BOTTOM'),
        ('GRID', (0, 0), (-1, -1), 0.5, 'black'),
    ]))

table.wrap(0, 0)
table.drawOn(document, 2.5*cm, 20*cm)

# Save document
document.save()

输出: