调整reportlab.platypus.Paragraph流动对象的大小?
我有一段代码,试图调整一个reportlab Platypus流动对象的字体大小,直到它适应我给定的高度。但是我发现,段落流动对象在每次递归循环中并没有保存它的style.fontSize属性,这导致Python出现了无限递归的问题。
def fits_space(w,h,aW,aH,p):
"""
Determines if inputted text fits in the space given for a paragraph
and if it doesn't, reduces the font-size until it does.
"""
if w<=aW and h<=aH:
# return font size to apply it to the para again
print "final font_size: %d" % p.style.fontSize
return p # now render the paragraph in the doctemplate
else:
p.style.fontSize -= 1
w,h = p.wrap(aW, aH)
fits_space(w,h,aW,aH,p)
def renderPage(name, text):
doc = SimpleDocTemplate("%s.pdf" % name)
parts = []
style = ParagraphStyle(name='fancy')
style.fontSize = 150
p = Paragraph(text, style)
aW = PAGE_WIDTH-4*inch # available width and height
aH = PAGE_HEIGHT-4*inch
w,h = p.wrap(aW, aH) # find required space
p = fits_space(w,h,aW,aH,p) # recursively fit the font size to the text
parts.append(p)
doc.build(parts)
有没有人能告诉我,为什么在fits_space()函数的else部分,当我调用p.wrap(aW, aH)时,输出的值和我把段落的字体大小减1之前是一样的?如果我减小字体大小,包裹后的高度不应该变小吗?
有没有人能帮我找出我哪里出错了?
更新
Nitzie的代码几乎能用,只是我还需要在style.leading上添加一个调整器:
def shrink_font_size(aW, aH, text, style):
"""Shrinks font size by using pdfmetrics to calculate the height
of a paragraph, given the font name, size, and available width."""
def break_lines(text, aW):
# simpleSplit calculates how reportlab will break up the lines for
# display in a paragraph, by using width/fontsize.
return simpleSplit(text, style.fontName, style.fontSize, aW)
def line_wrap(lines, style):
# Get overall width of text by getting stringWidth of longest line
width = stringWidth(max(lines), style.fontName, style.fontSize)
# Paragraph height can be calculated via line spacing and number of lines.
height = style.leading * len(lines)
return width, height
lines = break_lines(text, aW)
width, height = line_wrap(lines, style)
while height > aH or width > aW:
style.fontSize -= 1
style.leading -= 1 # do this if you're leading is based on the fontSize
lines = break_lines(text, aW)
width, height = line_wrap(lines, style)
谢谢大家!
1 个回答
5
不知道为什么,p.wrap
在你第一次调用后,如果你改变了字体大小,它似乎不会真正重新计算高度和宽度。
我找到了一种可行的方法,通过使用 ReportLab 的 stringWidth
和 splitLines
方法来计算字体大小。虽然这样做意味着它不再是递归的:
from reportlab.platypus import SimpleDocTemplate
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.units import inch
from reportlab.lib.pagesizes import A4
from reportlab.platypus import Paragraph
from reportlab.lib.utils import simpleSplit
from reportlab.pdfbase.pdfmetrics import stringWidth
def shrink_font_size(aW, aH, text, style):
"""Shrinks font size by using pdfmetrics to calculate the height
of a paragraph, given the font name, size, and available width."""
def break_lines(text, aW):
# simpleSplit calculates how reportlab will break up the lines for
# display in a paragraph, by using width/fontsize.
return simpleSplit(text, style.fontName, style.fontSize, aW)
def line_wrap(lines, style):
# Get overall width of text by getting stringWidth of longest line
width = stringWidth(max(lines), style.fontName, style.fontSize)
# Paragraph height can be calculated via line spacing and number of lines.
height = style.leading * len(lines)
return width, height
lines = break_lines(text, aW)
width, height = line_wrap(lines, style)
while height > aH or width > aW:
style.fontSize -= 1
lines = break_lines(text, aW)
width, height = line_wrap(lines, style)
def renderPage(name, text):
doc = SimpleDocTemplate("%s.pdf" % name)
parts = []
# Wasn't sure where PAGE_WIDTH and PAGE_HEIGHT came from for OP,
# so I just used a standard A4 page measurement.
PAGE_WIDTH, PAGE_HEIGHT = A4
aW = PAGE_WIDTH - 4*inch # available width and height
aH = PAGE_HEIGHT - 4*inch
style = ParagraphStyle(name='fancy')
style.fontSize = 200
style.leading = 20
shrink_font_size(aW, aH, text, style)
p = Paragraph(text, style)
parts.append(p)
doc.build(parts)
if __name__ == "__main__":
renderPage('test', '12345689019283382848248284 842828428529392381472754 842828428529392381472754 842828428529392381472754\n' * 10)