Führe PDF-Vorlagen für Dokumente ein: Titelseite, grafischer Kopf und Fuß, Angaben je Fassung (ADR-51, Nachtrag)
Deploy Test / test (push) Successful in 3m21s
Deploy Test / deploy (push) Successful in 27s

Eine Vorlage ist reines Layout (Titelseite mit allein stehendem Titel, Kopf- und Fußgrafik aus dem Medienbestand,
Herausgeber und Standardvermerk) und wird dem Dokument zugeordnet. Untertitel, Ausgabe, Veröffentlichungsdatum und
Copyright-Vermerk gehören zur Dokumentfassung (document_meta). Ohne Vorlage bleibt das PDF unverändert.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Sonnet 5.5 committed 2026-11-24 14:28:34 +01:00
1 parent 81037b9868
commit 765b8e779e
31 files changed
+1272 -33

No files matched your search

+7
View File
@@ -25,6 +25,7 @@ class Composition extends Model
'editorial_content_kind_id',
'archived_at',
'working_draft',
'pdf_template_id',
];
protected function casts(): array
@@ -46,6 +47,12 @@ public function editorialContentKind(): BelongsTo
return $this->belongsTo(EditorialContentKind::class);
}
/** Die PDF-Vorlage eines Dokuments; leer = bisheriges Layout (ADR-51). */
public function pdfTemplate(): BelongsTo
{
return $this->belongsTo(PdfTemplate::class);
}
public function versions(): HasMany
{
return $this->hasMany(CompositionVersion::class);
@@ -367,6 +367,7 @@ private function materialize(
'duration_max' => $didactic['duration_max'] ?? null,
'material_note' => $didactic['material_note'] ?? null,
'prerequisites' => $didactic['prerequisites'] ?? null,
'document_meta' => $didactic['document_meta'] ?? null,
],
$variants,
);
@@ -432,6 +433,7 @@ private function resolveTarget(string $target, ?User $actor, array $stack): Cont
'duration_max' => $draft['duration_max'] ?? null,
'material_note' => $draft['material_note'] ?? null,
'prerequisites' => $draft['prerequisites'] ?? null,
'document_meta' => $draft['document_meta'] ?? null,
],
$actor,
is_string($draft['revision_note'] ?? null) ? $draft['revision_note'] : null,
@@ -21,12 +21,14 @@ class CompositionVersion extends Model
'prerequisites',
'structure_hash',
'source_snapshot_id',
'document_meta',
];
protected function casts(): array
{
return [
'status' => ContentVersionStatus::class,
'document_meta' => 'array',
];
}
+5
View File
@@ -335,6 +335,7 @@ public function publishComposition(
'duration_max' => isset($didactic['duration_max']) ? (int) $didactic['duration_max'] : null,
'material_note' => $this->optionalText($didactic['material_note'] ?? null),
'prerequisites' => $this->optionalText($didactic['prerequisites'] ?? null),
'document_meta' => DocumentMeta::normalize($didactic['document_meta'] ?? null),
'source_snapshot_id' => isset($didactic['source_snapshot_id']) ? (int) $didactic['source_snapshot_id'] : null,
]);
@@ -394,6 +395,8 @@ public function structureHash(CompositionVersion $version): string
{
$version->loadMissing('members.variant', 'variants');
$meta = DocumentMeta::normalize($version->document_meta);
return hash('sha256', json_encode([
'fields' => [
$version->learning_goal,
@@ -402,6 +405,8 @@ public function structureHash(CompositionVersion $version): string
$version->material_note,
$version->prerequisites,
],
// Nur wenn gesetzt: Der Nachweis älterer Fassungen ohne Dokumentangaben bleibt unverändert.
...($meta === null ? [] : ['document' => $meta]),
'variants' => $version->variants->map(fn (CompositionVersionVariant $variant): array => [
$variant->key, $variant->position, $variant->name, $variant->description, $variant->origin,
])->all(),
+2
View File
@@ -56,6 +56,8 @@ public function build(User $user, LicenseArticle $article, ArticleRelease $relea
'document' => $document,
'title' => $text['title'],
'summary_html' => $text['summary_html'],
'pdf_template' => $document->composition->pdfTemplate,
'meta' => DocumentMeta::normalize($document->document_meta),
'chapters' => $chapters,
];
}
+51
View File
@@ -0,0 +1,51 @@
<?php
namespace App\Domain\Catalog;
use Carbon\CarbonImmutable;
/**
* Angaben einer Dokumentfassung für Titelseite und Impressum des PDF: Untertitel, Ausgabe (Release), Veröffentlichungsdatum und
* Copyright-Vermerk. Sie liegen als Teil der Fassung in `composition_versions.document_meta` und ändern sich nur mit einer neuen
* Fassung. Alle Felder sind Klartext; ein leeres Feld entfällt.
*/
final class DocumentMeta
{
public const FIELDS = ['subtitle', 'edition', 'published_on', 'copyright'];
/**
* Bereinigte Angaben: nur die bekannten Felder, getrimmt, ohne leere Werte; ein Datum nur als `YYYY-MM-DD`. Null, wenn nichts gesetzt ist.
*
* @return array<string, string>|null
*/
public static function normalize(mixed $value): ?array
{
if (! is_array($value)) {
return null;
}
$clean = [];
foreach (self::FIELDS as $field) {
$text = trim((string) ($value[$field] ?? ''));
if ($field === 'published_on') {
$text = preg_match('/^\d{4}-\d{2}-\d{2}$/', $text) === 1 ? $text : '';
}
if ($text !== '') {
$clean[$field] = mb_substr($text, 0, $field === 'copyright' ? 600 : 200);
}
}
return $clean === [] ? null : $clean;
}
/** Das Veröffentlichungsdatum in deutscher Schreibweise, oder leer. */
public static function publishedLabel(?array $meta): string
{
$date = $meta['published_on'] ?? '';
return $date === '' ? '' : CarbonImmutable::parse($date)->format('d.m.Y');
}
}
+160 -20
View File
@@ -3,14 +3,21 @@
namespace App\Domain\Catalog;
use App\Domain\Commerce\Receipt\PdfDocument;
use App\Domain\Media\PdfImage;
use Closure;
use Illuminate\Support\Facades\Storage;
use Transliterator;
/**
* Druckbare Ausgabe eines Dokuments (DIN A4) aus demselben Ergebnis wie die Ansicht ({@see DocumentGuide}): Titelseite mit
* anklickbarem Inhaltsverzeichnis samt Seitenzahlen, jedes Kapitel der ersten Ebene beginnt auf einer neuen Seite,
* Lesezeichen für die Gliederung des PDF-Betrachters. Fußzeile mit Lizenznehmer, Lizenzkopie und „Seite n von m“ auf jeder
* Seite (K2), die Kennung in den Metadaten (K1). Die Datei wird bei jedem Abruf neu erzeugt und nie abgelegt.
* Druckbare Ausgabe eines Dokuments (DIN A4) aus demselben Ergebnis wie die Ansicht ({@see DocumentGuide}): anklickbares
* Inhaltsverzeichnis samt Seitenzahlen, jedes Kapitel der ersten Ebene beginnt auf einer neuen Seite, Lesezeichen für die
* Gliederung des PDF-Betrachters. Fußzeile mit Lizenznehmer, Lizenzkopie und „Seite n von m“ auf jeder Seite (K2), die Kennung
* in den Metadaten (K1). Die Datei wird bei jedem Abruf neu erzeugt und nie abgelegt.
*
* Vorlage (ADR-51, Nachtrag): Ist dem Dokument eine {@see PdfTemplate} zugeordnet, steht der Titel allein auf einer eigenen
* Titelseite (mit Untertitel, Ausgabe, Veröffentlichungsdatum, Herausgeber und Copyright-Vermerk), und Kopf und Fuß aller
* weiteren Seiten tragen die gewählten Grafiken. Die Angaben kommen aus der Dokumentfassung ({@see DocumentMeta}). Ohne Vorlage
* beginnt das Inhaltsverzeichnis wie bisher unter dem Titel auf der ersten Seite.
*
* Seitenzahlen stehen erst nach dem Satz fest. Der Satz läuft deshalb so oft, bis Inhaltsverzeichnis, Kapitelanfänge und
* Seitenzahl sich nicht mehr ändern (in der Regel zweimal).
@@ -21,9 +28,7 @@ class DocumentPdf
private const RIGHT = 539.0;
private const TOP = 790.0;
private const BOTTOM = 72.0;
private const PAGE_HEIGHT = 842.0;
private PdfDocument $pdf;
@@ -35,20 +40,35 @@ class DocumentPdf
private int $total = 0;
private bool $onCover = false;
/** Obere und untere Grenze des Textes, abhängig von den Grafiken der Vorlage. */
private float $top = 790.0;
private float $bottom = 72.0;
/** 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>} */
private array $bands = ['header' => null, 'footer' => null, 'cover' => null];
/** @var array<string, array{page: int, top: float}> */
private array $starts = [];
/**
* @param array<string, mixed> $guide Ergebnis von {@see DocumentGuide::build}
* @param array<string, mixed> $guide Ergebnis von {@see DocumentGuide::build}; `pdf_template` und `meta` sind optional
*/
public function render(array $guide, string $licensee, string $copyId, ?PdfImages $images = null): string
{
$template = ($guide['pdf_template'] ?? null) instanceof PdfTemplate ? $guide['pdf_template'] : null;
$this->prepareLayout($template);
$starts = [];
$total = 0;
$bytes = '';
for ($pass = 0; $pass < 5; $pass++) {
$bytes = $this->pass($guide, $licensee, $copyId, $images, $starts, $total);
$bytes = $this->pass($guide, $template, $licensee, $copyId, $images, $starts, $total);
if ($this->starts === $starts && $this->total === $total) {
break;
@@ -61,30 +81,83 @@ public function render(array $guide, string $licensee, string $copyId, ?PdfImage
return $bytes;
}
/**
* Bereitet die Grafiken der Vorlage auf und legt Ränder fest: Kopf und Fuß in voller Seitenbreite (höchstens 140 Punkt hoch),
* der Text beginnt unter dem Kopf und endet über dem Fuß.
*/
private function prepareLayout(?PdfTemplate $template): void
{
$this->bands = [
'header' => $this->band($template?->headerVersion, 140.0),
'footer' => $this->band($template?->footerVersion, 140.0),
'cover' => $template !== null && $template->show_cover ? $this->band($template->coverVersion, self::PAGE_HEIGHT * 0.4) : null,
];
$header = (float) ($this->bands['header']['height'] ?? 0);
$footer = (float) ($this->bands['footer']['height'] ?? 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.
*
* @return array{jpeg: string, pixelWidth: int, pixelHeight: int, width: float, height: float, x: float}|null
*/
private function band(?ContentElementVersion $version, float $maxHeight): ?array
{
$file = $version === null ? null : EmbeddedImages::file($version);
$image = $file === null ? null : PdfImage::fromBytes((string) Storage::disk('media_derived')->get($file));
if ($image === null) {
return null;
}
$width = (float) PdfDocument::WIDTH;
$height = $width * $image['height'] / max(1, $image['width']);
if ($height > $maxHeight) {
$height = $maxHeight;
$width = $height * $image['width'] / max(1, $image['height']);
}
return ['jpeg' => $image['jpeg'], 'pixelWidth' => $image['width'], 'pixelHeight' => $image['height'], 'width' => $width, 'height' => $height, 'x' => (PdfDocument::WIDTH - $width) / 2];
}
/**
* @param array<string, mixed> $guide
* @param array<string, array{page: int, top: float}> $known Kapitelanfänge aus dem vorigen Durchgang
*/
private function pass(array $guide, string $licensee, string $copyId, ?PdfImages $images, array $known, int $total): string
private function pass(array $guide, ?PdfTemplate $template, string $licensee, string $copyId, ?PdfImages $images, array $known, int $total): string
{
$this->pdf = new PdfDocument;
$this->page = 0;
$this->total = 0;
$this->starts = [];
$this->onCover = false;
$this->footer = PdfFlow::printable('Lizenziert für '.$licensee.' · Lizenzkopie '.$copyId);
$this->flow = new PdfFlow($this->pdf, self::LEFT, self::RIGHT, self::TOP, self::BOTTOM, function (): void {
$this->flow = new PdfFlow($this->pdf, self::LEFT, self::RIGHT, $this->top, $this->bottom, function (): void {
$this->closePage();
$this->newPage();
}, $images === null ? null : Closure::fromCallable($images));
$knownTotal = $total;
$this->total = $knownTotal;
$this->newPage();
$this->total = $total;
$withCover = $template !== null && $template->show_cover;
if ($withCover) {
$this->onCover = true;
$this->newPage();
$this->cover($guide, $template);
$this->closePage();
$this->onCover = false;
$this->newPage();
} else {
$this->newPage();
$this->flow->write(PdfFlow::printable((string) $guide['title']), 24, true, 10);
}
$this->flow->write(PdfFlow::printable((string) $guide['title']), 24, true, 10);
$this->flow->write('Inhaltsverzeichnis', 14, true, 8, keep: 40);
foreach ($guide['chapters'] as $chapter) {
$this->contentsLine($chapter, $known[$chapter['anchor']] ?? ['page' => 1, 'top' => self::TOP]);
$this->contentsLine($chapter, $known[$chapter['anchor']] ?? ['page' => 1, 'top' => $this->top]);
}
foreach ($guide['chapters'] as $chapter) {
@@ -101,6 +174,63 @@ private function pass(array $guide, string $licensee, string $copyId, ?PdfImages
return $bytes;
}
/**
* Titelseite: Bild (falls gewählt), Titel allein, Untertitel, dann Ausgabe, Veröffentlichung und Herausgeber, unten der
* Copyright-Vermerk. Die Vermerke der Fassung gehen dem Standard der Vorlage vor.
*
* @param array<string, mixed> $guide
*/
private function cover(array $guide, PdfTemplate $template): void
{
$meta = is_array($guide['meta'] ?? null) ? $guide['meta'] : [];
$y = self::PAGE_HEIGHT - 150.0;
if ($this->bands['cover'] !== null) {
$cover = $this->bands['cover'];
$this->pdf->image($cover['x'], self::PAGE_HEIGHT - $cover['height'], $cover['width'], $cover['height'], $cover['jpeg'], $cover['pixelWidth'], $cover['pixelHeight']);
$y = self::PAGE_HEIGHT - $cover['height'] - 90.0;
}
$this->coverText(PdfFlow::printable((string) $guide['title']), 30.0, true, $y, 12.0);
if (filled($meta['subtitle'] ?? null)) {
$this->coverText(PdfFlow::printable((string) $meta['subtitle']), 15.0, false, $y, 8.0);
}
$y -= 26.0;
$facts = array_filter([
filled($meta['edition'] ?? null) ? $meta['edition'] : null,
DocumentMeta::publishedLabel($meta) !== '' ? 'Veröffentlicht am '.DocumentMeta::publishedLabel($meta) : null,
filled($template->publisher) ? 'Herausgeber: '.$template->publisher : null,
]);
foreach ($facts as $fact) {
$this->coverText(PdfFlow::printable((string) $fact), 11.0, false, $y, 4.0);
}
$copyright = trim((string) ($meta['copyright'] ?? '')) !== '' ? (string) $meta['copyright'] : (string) $template->copyright;
if (trim($copyright) !== '') {
$lines = PdfDocument::wrap(PdfFlow::printable($copyright), self::RIGHT - self::LEFT, 8.5, false);
$at = $this->footerLine + 24.0 + (count($lines) - 1) * 12.0;
foreach ($lines as $line) {
$this->pdf->text(self::LEFT, $at, $line, 8.5);
$at -= 12.0;
}
}
}
private function coverText(string $text, float $size, bool $bold, float &$y, float $after): void
{
foreach (PdfDocument::wrap($text, self::RIGHT - self::LEFT, $size, $bold) as $line) {
$this->pdf->text(self::LEFT, $y, $line, $size, $bold);
$y -= $size + 6.0;
}
$y -= $after;
}
/**
* Eine Zeile des Inhaltsverzeichnisses: Nummer und Titel, Punktlinie, Seitenzahl; die ganze Zeile springt zum Kapitel.
*
@@ -178,13 +308,23 @@ private function newPage(): void
$this->pdf->addPage();
$this->page++;
$this->flow->restart();
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']);
}
}
private function closePage(): void
{
$this->pdf->line(self::LEFT, 54, self::RIGHT, 54, 0.4);
$this->pdf->text(self::LEFT, 40, $this->footer, 8);
$this->pdf->textRight(self::RIGHT, 40, 'Seite '.$this->page.($this->total > 0 ? ' von '.$this->total : ''), 8);
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->line(self::LEFT, $this->footerLine, self::RIGHT, $this->footerLine, 0.4);
$this->pdf->text(self::LEFT, $this->footerLine - 14.0, $this->footer, 8);
$this->pdf->textRight(self::RIGHT, $this->footerLine - 14.0, 'Seite '.$this->page.($this->total > 0 ? ' von '.$this->total : ''), 8);
}
private function asciiTitle(string $title): string
+53
View File
@@ -0,0 +1,53 @@
<?php
namespace App\Domain\Catalog;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* Vorlage für das PDF eines Dokuments (ADR-51, Nachtrag): Titelseite, grafische Kopf- und Fußzeile und Standardvermerke.
* Die Vorlage bestimmt nur das Aussehen. Der Inhalt, auch Ausgabe, Datum und Copyright-Vermerk der Dokumentfassung, gehört
* zur Fassung ({@see DocumentMeta}); die Vorlage liefert nur den Standard-Vermerk, wenn die Fassung keinen eigenen hat.
*/
class PdfTemplate extends Model
{
public const KIND_DOCUMENT = 'dokument';
protected $fillable = [
'name',
'kind',
'show_cover',
'header_version_id',
'footer_version_id',
'cover_version_id',
'publisher',
'copyright',
];
protected function casts(): array
{
return ['show_cover' => 'boolean'];
}
public function headerVersion(): BelongsTo
{
return $this->belongsTo(ContentElementVersion::class, 'header_version_id');
}
public function footerVersion(): BelongsTo
{
return $this->belongsTo(ContentElementVersion::class, 'footer_version_id');
}
public function coverVersion(): BelongsTo
{
return $this->belongsTo(ContentElementVersion::class, 'cover_version_id');
}
public function compositions(): HasMany
{
return $this->hasMany(Composition::class);
}
}