hitobito-kjv/app/domain/kjv/export/pdf/membership_cards.rb
KJV Bautzen a23a6a7f8b Phase 1: Korrekturen nach Test, Doku 🤖
- Prüfseite: absolute Übersetzungsschlüssel
- Sammel-PDF: QR-Code ohne Haarfugen, mehr Ruhezone, IDs ohne pluck auf DISTINCT
- Spaltenkopf der Personenliste ohne Spitzname
- README, Changelog, Version 0.2.0

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 20:31:17 +02:00

194 lines
5.9 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 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 mit mehreren Mitgliedsausweisen pro Seite (Scheckkartenformat).
#
# Alle Maße und Feldpositionen kommen aus Settings.kjv.membership_card.print
# (config/settings.yml). Der gedruckte Ausweis trägt keinen Gültigkeitsstatus, nur das
# Ausstellungsjahr; verbindlich ist immer die Prüfung über den QR-Code.
#
# Aufbau einer Karte:
# - feste Elemente: #draw_static (Rahmen, Wappen, Vereinsname, Titel)
# - variable Felder: #draw_variable (Name, Mitgliedsnummer, Ausstellungsjahr, QR-Code)
# Für die spätere Designvorlage wird #draw_static ersetzt (z. B. Hintergrundbild).
class Kjv::Export::Pdf::MembershipCards
LOGO = "kjv_logo.png"
COLOR_TEXT = "222222"
COLOR_ACCENT = "365902"
COLOR_BORDER = "BBBBBB"
COLOR_CROP = "000000"
attr_reader :cards
def initialize(cards, issued_year: Date.current.year)
@cards = cards
@issued_year = issued_year
end
def render
cards.each_slice(per_page).with_index do |page_cards, index|
pdf.start_new_page if index.positive?
draw_crop_marks(page_cards.size) if config.crop_marks
page_cards.each_with_index { |card, slot| draw_card(card, slot) }
end
pdf.render
end
def filename
"mitgliedsausweise.pdf"
end
private
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 fields
config.fields
end
def per_page = columns * rows
def columns = config.columns.to_i
def rows = config.rows.to_i
def card_width = config.card_width.to_f.mm
def card_height = config.card_height.to_f.mm
def gap_x = config.gap_x.to_f.mm
def gap_y = config.gap_y.to_f.mm
def grid_width = (columns * card_width) + ((columns - 1) * gap_x)
def grid_height = (rows * card_height) + ((rows - 1) * gap_y)
def left = (pdf.bounds.width - grid_width) / 2
def top = pdf.bounds.height - ((pdf.bounds.height - grid_height) / 2)
# Linke obere Ecke der Karte im Seitenkoordinatensystem
def card_origin(slot)
column = slot % columns
row = slot / columns
[left + (column * (card_width + gap_x)), top - (row * (card_height + gap_y))]
end
def draw_card(card, slot)
pdf.bounding_box(card_origin(slot), width: card_width, height: card_height) do
draw_static
draw_variable(card)
end
end
def draw_static
if config.card_border
pdf.stroke_color COLOR_BORDER
pdf.line_width 0.3
pdf.stroke_bounds
end
logo = fields.logo
pdf.image logo_path, at: point(logo), width: logo.size.to_f.mm, height: logo.size.to_f.mm
text(fields.club_name, "Kreisjagdverband Bautzen e.V.", style: :bold, color: COLOR_ACCENT)
text(fields.title, "Mitgliedsausweis")
end
def draw_variable(card)
text(fields.name, card.name, style: :bold)
text(fields.member_number, "Mitgliedsnr. LJV: #{card.member_number.presence || "–"}")
text(fields.issued, "Ausgestellt #{@issued_year} · Prüfung per QR-Code")
draw_qr_code(card.qr_code, fields.qr_code)
end
def text(field, value, style: :normal, color: COLOR_TEXT)
pdf.fill_color color
pdf.text_box value.to_s,
at: point(field),
width: (field.width || (config.card_width.to_f - field.x.to_f - 3)).to_f.mm,
height: (field.font_size.to_f * 1.6),
size: field.font_size.to_f,
style: style,
overflow: :shrink_to_fit
pdf.fill_color COLOR_TEXT
end
# QR-Code als Vektorgrafik, damit er beim Druck scharf bleibt. Zusammenhängende dunkle
# Module einer Zeile werden als ein Rechteck gezeichnet, leicht überlappend, damit keine
# hellen Haarfugen zwischen den Modulen entstehen.
def draw_qr_code(qr_code, field)
modules = qr_code.qrcode.modules
module_size = field.size.to_f.mm / modules.size
overlap = module_size * 0.04
x, y = point(field)
pdf.fill_color "000000"
modules.each_with_index do |row, row_index|
dark_runs(row).each do |start, length|
pdf.fill_rectangle [x + (start * module_size), y - (row_index * module_size)],
(length * module_size) + overlap, module_size + overlap
end
end
pdf.fill_color COLOR_TEXT
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
# Feldposition (mm ab linker oberer Kartenecke) in Prawn-Koordinaten der Karte
def point(field)
[field.x.to_f.mm, card_height - field.y.to_f.mm]
end
def logo_path
HitobitoKjv::Wagon.root.join("app", "assets", "images", LOGO).to_s
end
# Schnittmarken außerhalb des Kartenrasters, in Verlängerung jeder Kartenkante
def draw_crop_marks(count)
used_rows = (count.to_f / columns).ceil
length = config.crop_mark_length.to_f.mm
offset = config.crop_mark_offset.to_f.mm
bottom = top - (used_rows * card_height) - ((used_rows - 1) * gap_y)
pdf.stroke_color COLOR_CROP
pdf.line_width 0.25
vertical_edges.each do |x|
pdf.stroke_line [x, top + offset], [x, top + offset + length]
pdf.stroke_line [x, bottom - offset], [x, bottom - offset - length]
end
horizontal_edges(used_rows, bottom).each do |y|
pdf.stroke_line [left - offset, y], [left - offset - length, y]
pdf.stroke_line [left + grid_width + offset, y], [left + grid_width + offset + length, y]
end
end
def vertical_edges
(0...columns).flat_map do |column|
x = left + (column * (card_width + gap_x))
[x, x + card_width]
end.uniq
end
def horizontal_edges(used_rows, bottom)
(0...used_rows).flat_map do |row|
y = top - (row * (card_height + gap_y))
[y, y - card_height]
end.push(bottom).uniq
end
end