ReportLab:将文本与keepinframe内部框架不起作用对齐
我正在尝试使用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()
Does anyone know how to fix this issue? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:我能够通过将
段落
与table
function(而不是frame
)相结合来解决此问题。根据 reportlab的文档(第68):
这是所需输出的工作示例:
输出:
Update: I was able to solve this problem by combining the
Paragraph
with theTable
functions (instead ofFrame
).According to ReportLab's documentation (page 68):
Here's a working example of the desired output:
Output: