如何使用 Reportlab 在单个段落内的行之间添加空格
我有一个从数据库动态提取的文本块,并在提供给用户之前将其放置在 PDF 中。文本被放置在有线条的背景上,就像记事本纸一样。我想对文本进行间隔,以便每条背景行之间只有一行文本。
我能够使用以下代码在段落之间创建垂直间距(用于生成 PDF 的另一部分)。
style = getSampleStyleSheet()['Normal']
style.fontName = 'Helvetica'
style.spaceAfter = 15
style.alignment = TA_JUSTIFY
story = [Paragraph(choice.value,style) for choice in chain(context['question1'].itervalues(),context['question2'].itervalues())]
generated_file = StringIO()
frame1 = Frame(50,100,245,240, showBoundary=0)
frame2 = Frame(320,100,245,240, showBoundary=0)
page_template = PageTemplate(frames=[frame1,frame2])
doc = BaseDocTemplate(generated_file,pageTemplates=[page_template])
doc.build(story)
但是,这在这里不起作用,因为我只有一个大段落。
I have a block of text that is dynamically pulled from a database and is placed in a PDF before being served to a user. The text is being placed onto a lined background, much like notepad paper. I want to space the text so that only one line of text is between each background line.
I was able to use the following code to create a vertical spacing between paragraphs (used to generate another part of the PDF).
style = getSampleStyleSheet()['Normal']
style.fontName = 'Helvetica'
style.spaceAfter = 15
style.alignment = TA_JUSTIFY
story = [Paragraph(choice.value,style) for choice in chain(context['question1'].itervalues(),context['question2'].itervalues())]
generated_file = StringIO()
frame1 = Frame(50,100,245,240, showBoundary=0)
frame2 = Frame(320,100,245,240, showBoundary=0)
page_template = PageTemplate(frames=[frame1,frame2])
doc = BaseDocTemplate(generated_file,pageTemplates=[page_template])
doc.build(story)
However, this won't work here because I have only a single, large paragraph.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很确定你想要改变的是领先的。来自用户手册第 6 章。
前导在第 2 章前面定义:
因此尝试不同的领先值,例如:
Pretty sure what yo u want to change is the leading. From the user manual in chapter 6.
Leading is defined earlier in chapter 2:
So try different values of leading, for example:
添加行距到 ParagraphStyle
生成 PDF
结果
Add leading to ParagraphStyle
Generate PDF
The result