ReportLab 段落格式换行符

ReportLab Paragraph Formatting Line Breaks

我是 ReportLab 的新手,我正在创建一个简单的文档,员工将文本输入到网站的文本区域中,将其保存到数据库中,然后从数据库中提取以通过 ReportLab 创建 PDF。我看到的问题是,如果用户输入的文本包含 2 个段落之间的换行符,则在创建 PDF 时不会遵守该换行符。

当我通过相同的 Python 脚本将数据库中的文本内容直接打印到屏幕上时,一切都是正确的:

Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

但是,在 PDF 中它看起来像:

Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

起初我以为是数据库中的文本格式问题,但在我能够在创建 PDF 之前正确打印出内容后,我知道那不是问题所在。

我是否缺少段落的简单样式输入?我有点迷茫,因为我还不知道 ReportLab 的所有来龙去脉。

这是脚本中的 ReportLab 代码片段。 employeeQuestion1 是在创建 PDF 之前以正确格式打印出来的内容。

def stylesheet():
    styles= {
        'default': ParagraphStyle(
            'default',
            fontName='SourceSansPro-Bold',
            fontSize=10,
            leading=12,
            leftIndent=0,
            rightIndent=0,
            firstLineIndent=0,
            alignment=TA_LEFT,
            spaceBefore=0,
            spaceAfter=0,
            bulletFontName='Times-Roman',
            bulletFontSize=10,
            bulletIndent=0,
            textColor= black,
            backColor=None,
            wordWrap=None,
            borderWidth= 0,
            borderPadding= 0,
            borderColor= None,
            borderRadius= None,
            allowWidows= 1,
            allowOrphans= 0,
            textTransform=None,
            endDots=None,         
            splitLongWords=1,
        ),
    }
    styles['employee_response'] = ParagraphStyle(
        'employee_response',
        parent=styles['default'],
        fontName='SourceSansPro-Regular',
        fontSize=10,
        spaceAfter=10,
        leftIndent=20
    )
    return styles

def build_flowables(stylesheet):
    return [
        Paragraph('{0}'.format(employeeQuestion1), stylesheet['employee_response']),
    ]

def build_pdf(filename, flowables):
    doc = SimpleDocTemplate(filename, 
        rightMargin=inch/2,
        leftMargin=inch/2,
        topMargin=inch/2,
        bottomMargin=inch/2,
        pagesize=letter,
    )

    doc.addPageTemplates(
        [
            PageTemplate(
                frames=[
                    Frame(
                        doc.leftMargin,
                        doc.bottomMargin,
                        doc.width,
                        doc.height,
                        id=None
                    ),
                ]
            ),
        ]
    )
    doc.build(flowables)
    build_pdf('/etc/review_app/reviews/{0}.pdf'.format(pdfFileName), build_flowables(stylesheet()))

感谢您的帮助!

我通过将新行转换为 <br /> 标签解决了这个问题:

str(employeeQuestion1).replace('\n','<br />\n')

希望这对遇到同样问题的人有所帮助