Erlaube in Kopf und Fuß einer PDF-Vorlage ein Logo links oder rechts
Deploy Test / test (push) Successful in 3m8s
Deploy Test / deploy (push) Successful in 26s

Kopf und Fuß haben je eine Anordnung: Band in voller Seitenbreite (Vorgabe, wie
bisher), Logo links oder Logo rechts am Textrand. Die Logohöhe ist einstellbar
(16 bis 100 Punkt, Vorgabe 40); das Logo ist höchstens halb so breit wie der Text,
der Text beginnt darunter beziehungsweise endet darüber. Die Titelseite bleibt
unberührt. Additive Migration mit Vorgaben; der vorherige Programmstand zeigt jede
Grafik weiter als Band.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Sonnet 5.5 committed 2026-11-24 16:34:17 +01:00
1 parent b942b5331a
commit 9f9370dbf8
10 files changed
+301 -20

No files matched your search

+47 -14
View File
@@ -49,7 +49,7 @@ class DocumentPdf
/** Höhe der Fußlinie über dem Seitenrand. */
private float $footerLine = 54.0;
/** @var array{header: ?array<string, mixed>, footer: ?array<string, mixed>, cover: ?array<string, mixed>} */
/** @var array{header: ?array<string, mixed>, footer: ?array<string, mixed>, cover: ?array<string, mixed>} Grafiken samt Maßen, siehe {@see self::band()} */
private array $bands = ['header' => null, 'footer' => null, 'cover' => null];
/** @var array<string, array{page: int, top: float}> */
@@ -87,23 +87,26 @@ public function render(array $guide, string $licensee, string $copyId, ?PdfImage
private function prepareLayout(?PdfTemplate $template): void
{
$this->bands = [
'header' => $this->band($template?->target('header'), 140.0),
'footer' => $this->band($template?->target('footer'), 140.0),
'cover' => $template !== null && $template->show_cover ? $this->band($template->target('cover'), self::PAGE_HEIGHT * 0.4) : null,
'header' => $this->band($template?->target('header'), 140.0, 'header', $template?->layout('header')),
'footer' => $this->band($template?->target('footer'), 140.0, 'footer', $template?->layout('footer')),
'cover' => $template !== null && $template->show_cover ? $this->band($template->target('cover'), self::PAGE_HEIGHT * 0.4, 'cover') : null,
];
$header = (float) ($this->bands['header']['height'] ?? 0);
$footer = (float) ($this->bands['footer']['height'] ?? 0);
$header = (float) ($this->bands['header']['occupied'] ?? 0);
$footer = (float) ($this->bands['footer']['occupied'] ?? 0);
$this->top = $header > 0 ? min(790.0, self::PAGE_HEIGHT - $header - 26.0) : 790.0;
$this->footerLine = max(54.0, $footer + 24.0);
$this->bottom = $this->footerLine + 18.0;
}
/**
* Eine Grafik in Seitenbreite, bei zu großer Höhe verkleinert und zentriert.
* Eine Grafik für Kopf, Fuß oder Titelseite. Als Band steht sie in Seitenbreite (bei zu großer Höhe verkleinert und zentriert),
* als Logo hat sie die gewählte Höhe und steht am linken oder rechten Textrand, höchstens halb so breit wie der Text.
* `occupied` ist die Höhe, die sie am Seitenrand belegt; `y` die Unterkante.
*
* @return array{jpeg: string, pixelWidth: int, pixelHeight: int, width: float, height: float, x: float}|null
* @param array{align: string, height: int}|null $layout nur für Kopf und Fuß
* @return array{jpeg: string, pixelWidth: int, pixelHeight: int, width: float, height: float, x: float, y: float, occupied: float}|null
*/
private function band(?string $target, float $maxHeight): ?array
private function band(?string $target, float $maxHeight, string $edge, ?array $layout = null): ?array
{
$bytes = TemplateImage::bytes($target);
$image = $bytes === null ? null : PdfImage::fromBytes($bytes);
@@ -112,15 +115,45 @@ private function band(?string $target, float $maxHeight): ?array
return null;
}
$ratio = $image['width'] / max(1, $image['height']);
$align = $layout['align'] ?? 'full';
$common = ['jpeg' => $image['jpeg'], 'pixelWidth' => $image['width'], 'pixelHeight' => $image['height']];
if ($edge !== 'cover' && $align !== 'full') {
$height = (float) ($layout['height'] ?? PdfTemplate::LOGO_DEFAULT);
$width = $height * $ratio;
if ($width > (self::RIGHT - self::LEFT) / 2) {
$width = (self::RIGHT - self::LEFT) / 2;
$height = $width / $ratio;
}
$padding = $edge === 'header' ? 24.0 : 14.0;
return $common + [
'width' => $width,
'height' => $height,
'x' => $align === 'left' ? self::LEFT : self::RIGHT - $width,
'y' => $edge === 'header' ? self::PAGE_HEIGHT - $padding - $height : $padding,
'occupied' => $height + $padding,
];
}
$width = (float) PdfDocument::WIDTH;
$height = $width * $image['height'] / max(1, $image['width']);
$height = $width / $ratio;
if ($height > $maxHeight) {
$height = $maxHeight;
$width = $height * $image['width'] / max(1, $image['height']);
$width = $height * $ratio;
}
return ['jpeg' => $image['jpeg'], 'pixelWidth' => $image['width'], 'pixelHeight' => $image['height'], 'width' => $width, 'height' => $height, 'x' => (PdfDocument::WIDTH - $width) / 2];
return $common + [
'width' => $width,
'height' => $height,
'x' => (PdfDocument::WIDTH - $width) / 2,
'y' => $edge === 'header' ? self::PAGE_HEIGHT - $height : 0.0,
'occupied' => $height,
];
}
/**
@@ -342,7 +375,7 @@ private function newPage(): void
if (! $this->onCover && $this->bands['header'] !== null) {
$band = $this->bands['header'];
$this->pdf->image($band['x'], self::PAGE_HEIGHT - $band['height'], $band['width'], $band['height'], $band['jpeg'], $band['pixelWidth'], $band['pixelHeight']);
$this->pdf->image($band['x'], $band['y'], $band['width'], $band['height'], $band['jpeg'], $band['pixelWidth'], $band['pixelHeight']);
}
}
@@ -350,7 +383,7 @@ private function closePage(): void
{
if (! $this->onCover && $this->bands['footer'] !== null) {
$band = $this->bands['footer'];
$this->pdf->image($band['x'], 0, $band['width'], $band['height'], $band['jpeg'], $band['pixelWidth'], $band['pixelHeight']);
$this->pdf->image($band['x'], $band['y'], $band['width'], $band['height'], $band['jpeg'], $band['pixelWidth'], $band['pixelHeight']);
}
$this->pdf->line(self::LEFT, $this->footerLine, self::RIGHT, $this->footerLine, 0.4);
+36
View File
@@ -25,6 +25,10 @@ class PdfTemplate extends Model
'header_element_id',
'footer_element_id',
'cover_element_id',
'header_align',
'footer_align',
'header_logo_height',
'footer_logo_height',
'publisher',
'copyright',
];
@@ -36,6 +40,38 @@ protected function casts(): array
public const SLOTS = ['header', 'footer', 'cover'];
/** Anordnung von Kopf- und Fußgrafik: Band in voller Seitenbreite oder Logo links oder rechts. */
public const ALIGNMENTS = ['full', 'left', 'right'];
public const LOGO_MIN = 16;
public const LOGO_MAX = 100;
public const LOGO_DEFAULT = 40;
/**
* Anordnung und Logohöhe (Punkt) von Kopf oder Fuß; ungültige Werte fallen auf die Vorgabe zurück.
*
* @return array{align: string, height: int}
*/
public function layout(string $slot): array
{
return self::normalizeLayout((string) $this->getAttribute($slot.'_align'), (int) $this->getAttribute($slot.'_logo_height'));
}
/**
* @return array{align: string, height: int}
*/
public static function normalizeLayout(?string $align, int|string|null $height): array
{
$height = (int) $height;
return [
'align' => in_array($align, self::ALIGNMENTS, true) ? (string) $align : 'full',
'height' => $height < 1 ? self::LOGO_DEFAULT : max(self::LOGO_MIN, min(self::LOGO_MAX, $height)),
];
}
/**
* Das Bild eines Bildfelds als Ziel des Pickers: `element:<Fassung>`, `medium:<Medium>` oder null.
*/
+38 -2
View File
@@ -44,6 +44,20 @@ class PdfVorlagen extends Page
public bool $showCover = true;
/**
* Anordnung von Kopf und Fuß: `full` (Band in voller Seitenbreite), `left` oder `right` (Logo).
*
* @var array<string, string>
*/
public array $anordnung = ['header' => 'full', 'footer' => 'full'];
/**
* Höhe des Logos in Punkt, wenn Kopf oder Fuß ein Logo tragen.
*
* @var array<string, int|string>
*/
public array $logohoehe = ['header' => PdfTemplate::LOGO_DEFAULT, 'footer' => PdfTemplate::LOGO_DEFAULT];
/**
* Die Bilder je Bildfeld (header, footer, cover) als Ziel des Pickers: `element:<Fassung>` oder `medium:<Medium>`.
*
@@ -107,14 +121,15 @@ public function changed(): bool
$template = $this->templateId > 0 ? PdfTemplate::query()->find($this->templateId) : null;
if (! $template instanceof PdfTemplate) {
return $this->name !== '' || $this->publisher !== '' || $this->copyright !== '' || array_filter($this->bilder) !== [];
return $this->name !== '' || $this->publisher !== '' || $this->copyright !== '' || array_filter($this->bilder) !== [] || $this->layouts() !== [PdfTemplate::normalizeLayout(null, null), PdfTemplate::normalizeLayout(null, null)];
}
return $this->name !== $template->name
|| $this->publisher !== (string) $template->publisher
|| $this->copyright !== (string) $template->copyright
|| $this->showCover !== (bool) $template->show_cover
|| $this->bilder !== $this->templateSlots($template);
|| $this->bilder !== $this->templateSlots($template)
|| $this->layouts() !== [$template->layout('header'), $template->layout('footer')];
}
public function save(): void
@@ -144,6 +159,10 @@ public function save(): void
...PdfTemplate::columns('header', $this->bilder['header']),
...PdfTemplate::columns('footer', $this->bilder['footer']),
...PdfTemplate::columns('cover', $this->bilder['cover']),
'header_align' => $this->layouts()[0]['align'],
'header_logo_height' => $this->layouts()[0]['height'],
'footer_align' => $this->layouts()[1]['align'],
'footer_logo_height' => $this->layouts()[1]['height'],
'publisher' => trim($this->publisher) === '' ? null : trim($this->publisher),
'copyright' => trim($this->copyright) === '' ? null : trim($this->copyright),
];
@@ -303,6 +322,21 @@ private function loadTemplate(PdfTemplate $template): void
$this->copyright = (string) $template->copyright;
$this->showCover = (bool) $template->show_cover;
$this->bilder = $this->templateSlots($template);
$this->anordnung = ['header' => $template->layout('header')['align'], 'footer' => $template->layout('footer')['align']];
$this->logohoehe = ['header' => $template->layout('header')['height'], 'footer' => $template->layout('footer')['height']];
}
/**
* Anordnung und Logohöhe von Kopf und Fuß, ungültige Eingaben auf gültige Werte gebracht.
*
* @return list<array{align: string, height: int}>
*/
private function layouts(): array
{
return [
PdfTemplate::normalizeLayout($this->anordnung['header'] ?? null, $this->logohoehe['header'] ?? null),
PdfTemplate::normalizeLayout($this->anordnung['footer'] ?? null, $this->logohoehe['footer'] ?? null),
];
}
/**
@@ -321,6 +355,8 @@ private function clearForm(): void
$this->copyright = '';
$this->showCover = true;
$this->bilder = ['header' => null, 'footer' => null, 'cover' => null];
$this->anordnung = ['header' => 'full', 'footer' => 'full'];
$this->logohoehe = ['header' => PdfTemplate::LOGO_DEFAULT, 'footer' => PdfTemplate::LOGO_DEFAULT];
}
protected function pickerExceptId(): int