Skip to content

Instantly share code, notes, and snippets.

@pietrocolombo
Created December 22, 2022 22:25
Show Gist options
  • Save pietrocolombo/f75b75ab5bd8f8b007a905a20385b021 to your computer and use it in GitHub Desktop.
Save pietrocolombo/f75b75ab5bd8f8b007a905a20385b021 to your computer and use it in GitHub Desktop.
example generation pdf (invoice) with reportlab
from reportlab.pdfgen import canvas
from reportlab.lib.units import mm
from reportlab.lib.pagesizes import A4
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase import pdfmetrics
from reportlab.lib.enums import TA_LEFT, TA_CENTER
from reportlab.lib.styles import ParagraphStyle
from reportlab.platypus import Paragraph
from reportlab.lib.styles import getSampleStyleSheet
import datetime
class pdf():
def __init__(self):
super().__init__()
# definition style
self.style_mid=ParagraphStyle('style_mid',
backColor='',
alignment=TA_LEFT,
fontSize=11,
borderColor='',
borderWidth=0,
borderPadding=(),
leading=17,
)
self.style_bold=ParagraphStyle('style_bold',
backColor='',
alignment=TA_CENTER,
fontSize=11,
borderColor='',
borderWidth=0,
borderPadding=(),
leading=20,
)
self.header_top = Paragraph('''What is Lorem Ipsum?''', self.style_bold)
self.mid_body = Paragraph('''
Lorem Ipsum is simply dummy text of the printing and typesetting industry. <BR/>
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, <BR/>
when an unknown printer took a galley of type and scrambled it to make a type specimen book. <BR/>
It has survived not only five centuries, but also the leap into electronic typesetting, <BR/>
remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset <BR/>
sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like <BR/>
Aldus PageMaker including versions of Lorem Ipsum.
''', self.style_mid)
self.center_mid = Paragraph('''Where does it come from?''', self.style_bold)
self.down_body = Paragraph('''
Contrary to popular belief, Lorem Ipsum is not simply random text. <BR/>
It has roots in a piece of classical Latin literature from 45 BC, <BR/>
making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, <BR/>
looked up one of the more obscure Latin words, consectetur,''', self.style_mid)
self.width, self.height=A4
def generation_pdf(self, path, data):
c = canvas.Canvas(path, pagesize=A4)
c.setFont("Helvetica", 11)
c.drawImage('logo.jpg', 20 * mm, 220 * mm, height=104, width=89)
#c.drawString(200,200 , 'Hello word')
c.drawImage('signature.png', 100 * mm, 10 * mm, height=66, width=134)
c.drawString(170*mm , 250*mm, 'N° ' + str(data[3].year))
self.header_top.wrapOn(c, 210*mm, 10*mm)
self.header_top.drawOn(c, 0*mm, 210*mm)
self.mid_body.wrapOn(c, 170*mm, 100*mm)
self.mid_body.drawOn(c, 25*mm, 140*mm)
self.center_mid.wrapOn(c, 210*mm, 10*mm)
self.center_mid.drawOn(c, 0*mm, 125*mm)
c.drawString(25*mm , 115*mm, '€')
c.drawString(45*mm , 115*mm, data[0])
c.drawString(25*mm , 105*mm, 'From:')
c.drawString(45*mm , 105*mm, data[1])
c.drawString(25*mm , 95*mm, 'TAX:')
c.drawString(45*mm , 95*mm, data[2])
c.drawString(25*mm , 85*mm, 'Date:')
c.drawString(45*mm , 85*mm, str(data[3]))
self.down_body.wrapOn(c, 170*mm, 50*mm)
self.down_body.drawOn(c, 25*mm, 35*mm)
c.showPage()
c.save()
if __name__ == '__main__':
pdf = pdf()
path = "test.pdf"
data = ("10",
"Elon Musk",
"000000000000",
datetime.date.today()
)
pdf.generation_pdf(path, data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment