python - ReportLab:如何自动调整文本大小以适合 block

标签 python pdf reportlab

我需要生成带有动态文本的 PDF,我正在使用 ReportLab。由于文本是动态的,是否可以调整其大小以适应 PDF 的特定区域?

最佳答案

从 reportlab 2.0 版开始,鸭嘴兽有 KeepInFrame。来自 CHANGES.txt:

KeepInFrame:
Sometimes the length of a piece of text you'd like to include in a 
fixed piece of page "real estate" is not guaranteed to be constrained to a 
fixed maximum length. In these cases, KeepInFrame allows you to specify an 
appropriate action to take when the text is too long for the space allocated 
for it. In particular, it can shrink the text to fit, mask (truncate) 
overflowing text, allow the text to overflow into the rest of the document, or 
raise an error.

我能找到的关于如何使用它的唯一示例是在 tests/ 中的 reportlab 源代码中。这是我最终想出的工作示例:

from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.pagesizes import letter, landscape
from reportlab.platypus import Paragraph, Frame, KeepInFrame
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import inch

c = Canvas('foo.pdf', pagesize=landscape(letter))
frame1 = Frame(0.25*inch, 0.25*inch, 4*inch, 4*inch, showBoundary=1)

styles = getSampleStyleSheet()
s = "foo bar " * 1000
story = [Paragraph(s, styles['Normal'])]
story_inframe = KeepInFrame(4*inch, 8*inch, story)
frame1.addFromList([story_inframe], c)
c.save()

以及完整性的版本字符串:

>python -c "import reportlab;print reportlab.Version"
2.7

关于python - ReportLab:如何自动调整文本大小以适合 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12014573/

相关文章:

python - 使用Shell脚本在python Shell中运行命令的意外行为

python - 将函数参数数据类型修复为 dataframe python

python - 使用 pyPdf 打开 pdf url

python - 如何将Figure对象转换为具有RGBA值的Numpy数组?

Python - 循环上的多个实例

python - Pyspark 从结构化流中的映射数组中提取值

python - 添加 __slots__ 后@property 不工作

android - 安卓支持adobe pdf和flash player吗

python - Matplotlib:带阴影线的 PDF 图形分辨率低

python - Reportlab 表拆分