Skip to content

Instantly share code, notes, and snippets.

@dingosaurus
Created Apr 15, 2019
Embed
What would you like to do?
reportlab font
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
FONT = os.path.join(settings.PROJECT_PATH, 'path/to/verah.ttf')
FONT_BOLD = os.path.join(settings.PROJECT_PATH, 'path/to/verahb.ttf')
FONT_NAME = "ticket_font"
FONT_BOLD_NAME = "ticket_font_bold"
FONT_FAMILY = "ticket_font_family"
pdfmetrics.registerFont(TTFont(FONT_BOLD_NAME, FONT_BOLD))
pdfmetrics.registerFont(TTFont(FONT_NAME, FONT))
pdfmetrics.registerFontFamily(FONT_FAMILY, normal=FONT_NAME, bold=FONT_BOLD_NAME)
# ...
c = canvas.Canvas(file_path, pagesize=A4)
c.setFont(FONT_NAME, 12)
c.setTitle(u"Název XYZ")
# SETUP P STYLE
styles = getSampleStyleSheet()
styles.add(ParagraphStyle(
name='NormalBlockText',
fontSize=11,
fontName=FONT_NAME,
# leading=13,
alignment=TA_JUSTIFY
))
# ...
p = Paragraph(unicode(action.text_1) or "", styles["NormalBlockText"])
w, h = p.wrap(180 * mm, 40 * mm)
p.drawOn(c, COL_1, 95 * mm - p.height)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment