如何使用 Reportlab 在单个段落内的行之间添加空格

发布于 2024-11-02 07:35:14 字数 753 浏览 0 评论 0原文

我有一个从数据库动态提取的文本块,并在提供给用户之前将其放置在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小苏打饼 2024-11-09 07:35:14

很确定你想要改变的是领先的。来自用户手册第 6 章。

要获得双倍行距文本,请使用高
领导。如果你设置
自动行距(默认“关闭”)
“min”(使用观察到的领先,即使
小于指定值)或“最大”(使用
观察到的和指定的较大者)
然后尝试确定
逐行领先。
如果行这可能很有用
包含不同的字体大小等。

前导在第 2 章前面定义:

行间距(行距)

点之间的垂直偏移
一行的起始位置和
下一个开始称为领先
偏移量。

因此尝试不同的领先值,例如:

style = getSampleStyleSheet()['Normal']
style.leading = 24

Pretty sure what yo u want to change is the leading. From the user manual in chapter 6.

To get double-spaced text, use a high
leading. If you set
autoLeading(default "off") to
"min"(use observed leading even if
smaller than specified) or "max"(use
the larger of observed and specified)
then an attempt is made to determine
the leading on a line by line basis.
This may be useful if the lines
contain different font sizes etc.

Leading is defined earlier in chapter 2:

Interline spacing (Leading)

The vertical offset between the point
at which one line starts and where the
next starts is called the leading
offset.

So try different values of leading, for example:

style = getSampleStyleSheet()['Normal']
style.leading = 24
淡写薰衣草的香 2024-11-09 07:35:14

添加行距到 ParagraphStyle

orden = ParagraphStyle('orden')
orden.leading = 14
orden.borderPadding = 10
orden.backColor=colors.gray
orden.fontSize = 14

生成 PDF

buffer = BytesIO()
p = canvas.Canvas(buffer, pagesize=letter)

text = Paragraph("TEXT Nro 0001", orden)
text.wrapOn(p,500,10)
text.drawOn(p, 45, 200)


p.showPage()
p.save()
pdf = buffer.getvalue()
buffer.close()

结果
输入图片此处描述

Add leading to ParagraphStyle

orden = ParagraphStyle('orden')
orden.leading = 14
orden.borderPadding = 10
orden.backColor=colors.gray
orden.fontSize = 14

Generate PDF

buffer = BytesIO()
p = canvas.Canvas(buffer, pagesize=letter)

text = Paragraph("TEXT Nro 0001", orden)
text.wrapOn(p,500,10)
text.drawOn(p, 45, 200)


p.showPage()
p.save()
pdf = buffer.getvalue()
buffer.close()

The result
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文