I am new to ReportLab and I am creating a simple document where employees enter text into a textarea on a website, it is saved into a DB, and then pulled from the DB to create a PDF via ReportLab. The issue I am seeing is that if a user enters text that contains a line break between 2 paragraphs, that line break is not honored when the PDF is created.
When I print the text content out of the DB directly to the screen via the same Python script, everything is correct:
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.
However, in the PDF it looks like:
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.
At first I thought it was a text formatting issue into the DB, but after I am able to print out the content correctly right before PDF creation I know thats not the problem.
Am I missing a simple style input for the paragraph? I am a little lost since I don't know all the ins and outs of ReportLab yet.
Here is the ReportLab code snippet from the script. employeeQuestion1
is what prints out in the correct format before the PDF is created.
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()))
Thanks for the help!
I solved this by converting new lines to <br />
tags:
str(employeeQuestion1).replace('\n','<br />\n')
Hope this helps someone with the same issue