ReportLab:将文本与keepinframe内部框架不起作用对齐

发布于 2025-01-21 23:29:33 字数 1518 浏览 0 评论 0原文

我正在尝试使用ReportLab在A frame内部对文本进行对齐。

问题在于,即使明确定义参数halign ='center' and valign ='底部' keepinframe函数,这不会更改默认的左水平对齐和顶部垂直对准顶部。

我使用的是最新版本的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()

输出:

有人知道如何解决此问题吗?提前致谢。

I am trying to align a text horizontally and vertically inside a Frame using ReportLab.

The problem is that even defining explicitly the arguments hAlign='CENTER' and vAlign='BOTTOM' for the KeepInFrame function, this does not change the default left horizontal alignment and top vertical alignment top.

I am using the latest version of ReportLab (reportlab==3.6.9).

Here's a code example:

# 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()

Output:
enter image description here

Does anyone know how to fix this issue? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余罪 2025-01-28 23:29:33

更新:我能够通过将段落table function(而不是frame)相结合来解决此问题。

根据 reportlab的文档(第68)

所有流量都有Halign属性:('左','右',“中心”
或“中心”)。对于填充框架的完整宽度的段落,
这没有效果。对于表格,图像或其他较少的对象
比框架的宽度,这决定了它们的水平
放置。

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

# 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()

输出:

Update: I was able to solve this problem by combining the Paragraph with the Table functions (instead of Frame).

According to 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.

Here's a working example of the desired output:

# 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()

Output:
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文