9

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

4

2 回答 2

8

从 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.

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

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
于 2014-01-22T21:29:43.060 回答
1

是的。查看 ReportLab 手册。根据您对您想要做什么的(简短)描述,听起来您需要查看在页面布局中使用框架(假设您使用鸭嘴兽,我强烈推荐)。

于 2012-08-18T18:33:46.537 回答