229 lines
7.3 KiB
Ruby
229 lines
7.3 KiB
Ruby
# frozen_string_literal: true
|
||
|
||
# Copyright (c) 2026, Kreisjagdverband Bautzen e.V. This file is part of
|
||
# hitobito_kjv and licensed under the Affero General Public License version 3
|
||
# or later. See the COPYING file at the top-level directory.
|
||
|
||
# Druckbogen für Mitgliedsausweise im Duplexdruck nach der Vorlage
|
||
# "MASTER_Mitgliedsausweis_KJV_schlicht_Herlitz_502041-7": je Bogen eine Seite mit
|
||
# Vorderseiten (fest: Wappen, Vereinsname, "MITGLIEDSAUSWEIS") und eine Seite mit
|
||
# Rückseiten (variabel: Name, Geburtsdatum, Eintritt, Mitglieds-Nr., QR-Code).
|
||
#
|
||
# Alle Maße, Positionen und Farben kommen aus Settings.kjv.membership_card.print
|
||
# (config/settings.yml). Der gedruckte Ausweis trägt keinen Gültigkeitsstatus, nur das
|
||
# Ausstellungsjahr; verbindlich ist die Prüfung über den QR-Code.
|
||
class Kjv::Export::Pdf::MembershipCards
|
||
FONT = "LiberationSans" # metrisch gleich Helvetica/Arial, mit Umlauten und Sorbisch
|
||
|
||
attr_reader :cards
|
||
|
||
def initialize(cards, issued_year: Date.current.year)
|
||
@cards = cards
|
||
@issued_year = issued_year
|
||
end
|
||
|
||
def render
|
||
pdf.font FONT
|
||
cards.each_slice(per_page).with_index do |sheet_cards, index|
|
||
pdf.start_new_page if index.positive?
|
||
render_side(sheet_cards, :front)
|
||
pdf.start_new_page
|
||
render_side(sheet_cards, :back)
|
||
end
|
||
pdf.render
|
||
end
|
||
|
||
def filename
|
||
"mitgliedsausweise.pdf"
|
||
end
|
||
|
||
private
|
||
|
||
def render_side(sheet_cards, side)
|
||
draw_crop_marks if config.crop_marks
|
||
sheet_cards.each_with_index do |card, slot|
|
||
pdf.bounding_box(card_origin(slot, side), width: card_width, height: card_height) do
|
||
(side == :front) ? draw_front : draw_back(card)
|
||
end
|
||
end
|
||
end
|
||
|
||
### Vorderseite (fest)
|
||
|
||
def draw_front
|
||
front = config.front
|
||
draw_card_background(colors.front_background, front.frame_width)
|
||
draw_logo(front.logo)
|
||
text(front.club_name, "Kreisjagdverband", style: :bold)
|
||
text(front.club_city, "BAUTZEN e.V.", style: :bold)
|
||
|
||
line = front.line
|
||
pdf.stroke_color colors.border
|
||
pdf.line_width line.line_width.to_f
|
||
pdf.stroke_horizontal_line mm(line.x), mm(line.x) + mm(line.width), at: y(line.y)
|
||
|
||
text(front.title, "MITGLIEDSAUSWEIS", style: :bold, x: 0, width: card_width,
|
||
align: :center)
|
||
end
|
||
|
||
### Rückseite (variabel)
|
||
|
||
def draw_back(card)
|
||
back = config.back
|
||
draw_card_background(colors.back_background, back.frame_width)
|
||
draw_logo(back.logo)
|
||
text(back.header, "Kreisjagdverband BAUTZEN e.V.", style: :bold)
|
||
|
||
rows = back.rows
|
||
row("Name:", card.list_name, rows.name, style: :bold)
|
||
row("Geburtsdatum:", format_date(card.birthday), rows.birthday)
|
||
row("Eintritt:", format_date(card.joined_on), rows.joined_on)
|
||
row("Mitglieds-Nr.:", card.member_number, rows.member_number, style: :bold)
|
||
|
||
draw_qr_code(card.qr_code, back.qr_code)
|
||
text(back.issued, "Ausgestellt #{@issued_year}
|
||
Prüfung per QR-Code",
|
||
color: colors.text, align: :center, lines: 2)
|
||
end
|
||
|
||
def row(label, value, field, style: :normal)
|
||
back = config.back
|
||
label_field = {y: field.label_y || (field.y.to_f + 0.18), font_size: back.label_font_size}
|
||
text(Config::Options.new(label_field), label, x: mm(back.label_x), color: colors.value)
|
||
text(field, value.presence || "–", x: mm(back.value_x), width: mm(back.value_width),
|
||
style: style, color: colors.value)
|
||
end
|
||
|
||
### Bausteine
|
||
|
||
def draw_card_background(fill, frame_width)
|
||
pdf.fill_color fill
|
||
pdf.fill_rectangle [0, card_height], card_width, card_height
|
||
|
||
frame = config.frame
|
||
inset = mm(frame.inset)
|
||
pdf.stroke_color colors.border
|
||
pdf.line_width frame_width.to_f
|
||
pdf.stroke_rounded_rectangle [inset, card_height - inset],
|
||
card_width - (2 * inset), card_height - (2 * inset), mm(frame.radius)
|
||
end
|
||
|
||
def draw_logo(field)
|
||
size = mm(field.size)
|
||
pdf.image logo_path, at: [mm(field.x), y(field.y)], width: size, height: size
|
||
end
|
||
|
||
def text(field, value, style: :normal, color: colors.text, x: nil, width: nil, align: :left,
|
||
lines: 1)
|
||
x ||= mm(field.x)
|
||
width ||= field.width ? mm(field.width) : card_width - x - mm(3)
|
||
pdf.fill_color color
|
||
pdf.text_box value.to_s,
|
||
at: [x, y(field.y)],
|
||
width: width,
|
||
height: field.font_size.to_f * 1.4 * lines,
|
||
size: field.font_size.to_f,
|
||
style: style,
|
||
align: align,
|
||
overflow: :shrink_to_fit
|
||
end
|
||
|
||
# QR-Code als Vektorgrafik. Zusammenhängende dunkle Module einer Zeile werden als ein
|
||
# Rechteck gezeichnet, leicht überlappend, damit keine hellen Haarfugen entstehen.
|
||
def draw_qr_code(qr_code, field)
|
||
modules = qr_code.qrcode.modules
|
||
module_size = mm(field.size) / modules.size
|
||
overlap = module_size * 0.04
|
||
left = mm(field.x)
|
||
top = y(field.y)
|
||
|
||
pdf.fill_color "000000"
|
||
modules.each_with_index do |row, row_index|
|
||
dark_runs(row).each do |start, length|
|
||
pdf.fill_rectangle [left + (start * module_size), top - (row_index * module_size)],
|
||
(length * module_size) + overlap, module_size + overlap
|
||
end
|
||
end
|
||
end
|
||
|
||
# [[Startspalte, Länge], ...] der dunklen Abschnitte einer Zeile
|
||
def dark_runs(row)
|
||
row.each_with_index.chunk_while { |(a, _), (b, _)| a == b }
|
||
.select { |run| run.first.first }
|
||
.map { |run| [run.first.last, run.size] }
|
||
end
|
||
|
||
def format_date(date)
|
||
date && I18n.l(date)
|
||
end
|
||
|
||
### Raster
|
||
|
||
def pdf
|
||
@pdf ||= Export::Pdf::Document.new(page_size: config.page_size, margin: 0).pdf
|
||
end
|
||
|
||
def config
|
||
Settings.kjv.membership_card.print
|
||
end
|
||
|
||
def colors
|
||
config.colors
|
||
end
|
||
|
||
def mm(value)
|
||
value.to_f.mm
|
||
end
|
||
|
||
# Abstand von der oberen Kartenkante in Prawn-Koordinaten der Karte
|
||
def y(from_top)
|
||
card_height - mm(from_top)
|
||
end
|
||
|
||
def per_page = columns * rows
|
||
|
||
def columns = config.columns.to_i
|
||
|
||
def rows = config.rows.to_i
|
||
|
||
def card_width = mm(config.card_width)
|
||
|
||
def card_height = mm(config.card_height)
|
||
|
||
# Linke obere Ecke der Karte im Seitenkoordinatensystem. Rückseiten werden für den
|
||
# Duplexdruck spaltenweise gespiegelt, damit Vorder- und Rückseite zusammenpassen.
|
||
def card_origin(slot, side)
|
||
column = slot % columns
|
||
row = slot / columns
|
||
column = columns - 1 - column if side == :back && config.mirror_back_columns
|
||
[
|
||
mm(config.origin_x) + (column * (card_width + mm(config.gap_x))),
|
||
pdf.bounds.height - mm(config.origin_y) - (row * (card_height + mm(config.gap_y)))
|
||
]
|
||
end
|
||
|
||
def logo_path
|
||
HitobitoKjv::Wagon.root.join("app", "assets", "images", config.logo).to_s
|
||
end
|
||
|
||
# Schnittmarken außerhalb des Kartenrasters (für Bögen ohne Perforation)
|
||
def draw_crop_marks
|
||
length = mm(config.crop_mark_length)
|
||
offset = mm(config.crop_mark_offset)
|
||
left = mm(config.origin_x)
|
||
top = pdf.bounds.height - mm(config.origin_y)
|
||
right = left + (columns * card_width) + ((columns - 1) * mm(config.gap_x))
|
||
bottom = top - (rows * card_height) - ((rows - 1) * mm(config.gap_y))
|
||
|
||
pdf.stroke_color "000000"
|
||
pdf.line_width 0.25
|
||
(0..columns).map { |c| left + (c * card_width) }.each do |x|
|
||
pdf.stroke_line [x, top + offset], [x, top + offset + length]
|
||
pdf.stroke_line [x, bottom - offset], [x, bottom - offset - length]
|
||
end
|
||
(0..rows).map { |r| top - (r * card_height) }.each do |y|
|
||
pdf.stroke_line [left - offset, y], [left - offset - length, y]
|
||
pdf.stroke_line [right + offset, y], [right + offset + length, y]
|
||
end
|
||
end
|
||
end
|