ReportLab:如何自动调整文本大小以适合块 [英] ReportLab: How to auto resize text to fit block

查看:578
本文介绍了ReportLab:如何自动调整文本大小以适合块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to generate a PDF with dynamic text and I'm using ReportLab. Since the text is dynamic, is there anyway to have it resized to fit within a specific area of the PDF?

推荐答案

从reportlab 2.0版platypus开始具有KeepInFrame.从CHANGES.txt:

Starting in reportlab version 2.0 platypus has KeepInFrame. From the 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源代码中找到有关如何使用它的示例.这是我最后想出的工作示例:

The only examples I could find on how to use it are in the reportlab source code in the tests/. Here is the working example I finally came up with:

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

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

And the version string for completeness:

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

这篇关于ReportLab:如何自动调整文本大小以适合块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆