alpinesmuseum-public/catalog/labels.py

176 lines
6.9 KiB
Python

import os
import reportlab
from reportlab.lib.pagesizes import letter, A6, A7
from reportlab.pdfgen import canvas
from reportlab.lib.units import cm
from reportlab.platypus import Paragraph, Frame, KeepInFrame
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.enums import TA_RIGHT
from django.conf import settings
LABEL_FORMAT = (7.62 * cm,
10.16 * cm)
def register_font():
# register fonts
mg_path = os.path.join(settings.BASE_DIR, "fonts", "MonumentGrotesk")
reportlab.rl_config.TTFSearchPath.append(mg_path)
fonts = [
"MonumentGrotesk-Medium",
"MonumentGrotesk-Regular",
"MonumentGrotesk-Semi-Mono",
]
for font in fonts:
pdfmetrics.registerFont(TTFont(font, "{}.ttf".format(font)))
def create_label_1(filename, inventory_nr, title, date, participant, categories, description):
label = canvas.Canvas(filename, pagesize=LABEL_FORMAT)
width, height = LABEL_FORMAT # keep for later
small_font_size = 7
small_font_name = "MonumentGrotesk-Semi-Mono"
showBoundary = 0
full_width = 6.9 * cm
width_inframe = 6.9 * cm
left_align = 15
# conf labels
inventory_nr_label = "ID"
date_label = "Datierung"
title_label = "Titel"
participant_label = "Herkunft"
categories_label = "Kategorie"
desc_label = "Spezielles"
content_style = ParagraphStyle(
name="Content",
fontName="MonumentGrotesk-Regular",
fontSize=14.0,
leading=15.5,
allowOrphans=0,
allowWidows=0
)
date_style = ParagraphStyle(
name="Content",
fontName="MonumentGrotesk-Regular",
fontSize=14.0,
leading=15.0,
alignment=TA_RIGHT,
allowOrphans=0,
allowWidows=0
)
title_style = ParagraphStyle(
name="Title",
fontName="MonumentGrotesk-Regular",
fontSize=50.0,
leading=52.0,
)
# - ID --------------------------------------------------------------------
label.setFont(small_font_name, small_font_size)
label.drawString(10, height - 15, inventory_nr_label)
# label.drawString(25,height-40, inventory_nr)
id_length = 4.1 * cm
frame1 = Frame(25, height - 45, id_length, 1.8 * cm, showBoundary=showBoundary, leftPadding=0, rightPadding=0, topPadding=0, bottomPadding=0)
styles = getSampleStyleSheet()
story = [Paragraph(inventory_nr, title_style)]
story_inframe = KeepInFrame(id_length, 1.8 * cm, story)
frame1.addFromList([story_inframe], label)
label.line(0.2 * cm, height - 50, width - 0.2 * cm, height - 50)
label.line(150, height - 10, 150, height - 45)
# - Datierung -------------------------------------------------------------
label.setFont(small_font_name, small_font_size)
label.drawString(163, height - 15, date_label)
frame1 = Frame(153, height - 51, 2.0 * cm, 0.7 * cm, showBoundary=showBoundary, leftPadding=0, rightPadding=0, topPadding=0, bottomPadding=0)
styles = getSampleStyleSheet()
story = [Paragraph(date, date_style)]
story_inframe = KeepInFrame(2 * cm, 0.7 * cm, story)
frame1.addFromList([story_inframe], label)
# - Title ---------------------------------------------------
label.setFont(small_font_name, small_font_size)
label.drawString(10, height - 60, title_label)
frame1 = Frame(left_align, height - 79, full_width, 0.7 * cm, showBoundary=showBoundary, leftPadding=2, rightPadding=2, topPadding=0, bottomPadding=0)
styles = getSampleStyleSheet()
story = [Paragraph("{}".format(title), content_style)]
story_inframe = KeepInFrame(width_inframe, 0.7 * cm, story)
frame1.addFromList([story_inframe], label)
label.line(0.2 * cm, height - 78, width - 0.2 * cm, height - 78)
# - BESITZER ------------------------------------------------
label.setFont(small_font_name, small_font_size)
label.drawString(10, height - 86, participant_label)
frame1 = Frame(left_align, height - 108, full_width, 0.8 * cm, showBoundary=showBoundary, leftPadding=2, rightPadding=2, topPadding=0, bottomPadding=0)
styles = getSampleStyleSheet()
story = [Paragraph("{}".format(participant), content_style)]
story_inframe = KeepInFrame(width_inframe, 0.8 * cm, story)
frame1.addFromList([story_inframe], label)
label.line(0.2 * cm, height - 106, width - 0.2 * cm, height - 106)
# - KATEGORIE -------------------------------------------------------
label.setFont(small_font_name, small_font_size)
label.drawString(10, height - 114, categories_label)
frame1 = Frame(left_align, height - 150, full_width, 1.3 * cm, showBoundary=showBoundary, leftPadding=2, rightPadding=2, topPadding=0, bottomPadding=0)
styles = getSampleStyleSheet()
story = [Paragraph(categories, content_style)]
story_inframe = KeepInFrame(width_inframe, 1.3 * cm, story)
frame1.addFromList([story_inframe], label)
label.line(0.2 * cm, height - 149, width - 0.2 * cm, height - 149)
# --------------------------------------------------------
label.setFont(small_font_name, small_font_size)
label.drawString(10, height - 157, desc_label)
frame1 = Frame(left_align, 2, full_width, 4.6 * cm, showBoundary=showBoundary, leftPadding=2, rightPadding=2, topPadding=0, bottomPadding=0)
styles = getSampleStyleSheet()
s = description
story = [Paragraph(s, content_style)]
story_inframe = KeepInFrame(width_inframe, 4.6 * cm, story)
frame1.addFromList([story_inframe], label)
label.save()
def create_label_3(filename, notes, participant):
label = canvas.Canvas(filename, pagesize=LABEL_FORMAT)
width, height = LABEL_FORMAT # keep for later
small_font_size = 7
small_font_name = "MonumentGrotesk-Semi-Mono"
label.setFont(small_font_name, small_font_size)
content_style = ParagraphStyle(
name="Content",
fontName="MonumentGrotesk-Regular",
fontSize=14.0,
leading=15.5,
allowOrphans=0,
allowWidows=0
)
header_style = ParagraphStyle(
name="Content",
fontName="MonumentGrotesk-Regular",
fontSize=8.0,
leading=9.0,
allowOrphans=0,
allowWidows=0
)
showBoundary = 0
# --------------------------------------------------------
frame1 = Frame(10, height-20, 7.0 * cm, 0.6 * cm, showBoundary=showBoundary, leftPadding=0, bottomPadding=0,)
story = [Paragraph(participant, header_style)]
story_inframe = KeepInFrame(7 * cm, 0.6 * cm, story)
frame1.addFromList([story_inframe], label)
frame2 = Frame(10, 2, 7.0 * cm, 9.5 * cm, showBoundary=showBoundary, leftPadding=0, bottomPadding=0, topPadding=2)
story = [Paragraph(notes, content_style)]
story_inframe = KeepInFrame(7 * cm, 9.5 * cm, story)
frame2.addFromList([story_inframe], label)
# --------------------------------------------------------
label.save()