Skip to content

Instantly share code, notes, and snippets.

@cwebber314
Created January 20, 2014 04:34
Show Gist options
  • Save cwebber314/8514907 to your computer and use it in GitHub Desktop.
Save cwebber314/8514907 to your computer and use it in GitHub Desktop.
Reportlab hello world with an image, bulleted list, and enumerated list.
"""
Reportlab sandbox.
"""
from reportlab.lib.enums import TA_JUSTIFY
from reportlab.lib.pagesizes import letter, landscape
from reportlab.lib.enums import TA_JUSTIFY
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
doc = SimpleDocTemplate("form_letter.pdf",pagesize=landscape(letter),
rightMargin=72,leftMargin=72,
topMargin=72,bottomMargin=18)
styles = getSampleStyleSheet()
styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY))
Story=[]
logo = "image.png"
# We really want to scale the image to fit in a box and keep proportions.
im = Image(logo, 3*inch, 3*inch)
Story.append(im)
#ptext = '<font size=12>Some text</font>'
#Story.append(Paragraph(ptext, styles["Normal"]))
ptext = '''
<seq>. </seq>Some Text<br/>
<seq>. </seq>Some more test Text
'''
Story.append(Paragraph(ptext, styles["Bullet"]))
ptext='<bullet>&bull;</bullet>Some Text'
Story.append(Paragraph(ptext, styles["Bullet"]))
doc.build(Story)
@shubhamyedage
Copy link

Hey I am building pdf with same approach. Is there any way to add space before text at new line by providing some value?

@Ronald-Saunfe
Copy link

Yes you can use spacers like this
Story.append(Spacer(1,0.2*inch))

@walkershashi
Copy link

I am building a pdf with an Image to the right and text to its left. But the text is added to the next line of the Image. How can I do the same can anyone help me out in this?

@katbormann
Copy link

katbormann commented Dec 30, 2020

I got my image (logo) justified to the right by using the hAlign parameter following
https://www.kite.com/python/docs/reportlab.platypus.Image
im = Image(file_logo, 0.75*inch, 0.75*inch, hAlign='RIGHT')
flowables.append(im)

@ArjunHS
Copy link

ArjunHS commented Dec 29, 2021

Do you know how to add space between table objects? I used "Story.append(Spacer(1,0.2*inch))" mentioned in the above comment but it didn't change anything in the pdf.

@abubelinha
Copy link

@ArjunHS this worked for me:

from reportlab.platypus import Paragraph
Story.append(table1)
Story.append(Paragraph("<br /><br />"))
Story.append(table2)

@ArjunHS
Copy link

ArjunHS commented Jan 4, 2022

@abubelinha Thanks for the code. This works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment