How to add space between lines within a single paragraph with Reportlab

Arion picture Arion · Apr 19, 2011 · Viewed 10.7k times · Source

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.

Answer

dting picture dting · Apr 19, 2011

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