Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 923
Arion Avatar asked Apr 18 '11 23:04

Arion


People also ask

How do you put a space between lines in Python?

You have a few solutions: The simpler one would be to use a print statement between your lines, which, used alone, prints a line feed. Alternatively, you could you end the string you're printing with a '\n' character, which will cause a line feed, or you could start your input with the same character.

Is ReportLab free?

ReportLab is a free open-source document creation engine for generating PDF documents and custom vector graphics.


1 Answers

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
like image 192
dting Avatar answered Sep 17 '22 11:09

dting