ReportLab段落格式换行

2024-05-29 11:27:04 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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的所有细节。在

这是来自实验室的代码片段。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()))

谢谢你的帮助!在


Tags: andoftheinfromisbcipsum

热门问题