ReportLab:如何自动调整文本大小以适应块

时间:2012-08-17 23:56:45

标签: python pdf reportlab

我需要使用动态文本生成PDF并且我正在使用ReportLab。由于文本是动态的,无论如何都要调整大小以适应PDF的特定区域吗?

2 个答案:

答案 0 :(得分:6)

从reportlab 2.0版开始,platypus有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

答案 1 :(得分:-1)

是。请查看ReportLab手册。基于你对你想要做什么的(简短)描述听起来你需要在页面布局中使用框架(假设你使用Platypus,我强烈推荐)。