Mache Anzeige und Tiefe des PDF-Inhaltsverzeichnisses in der Vorlage einstellbar
Die PDF-Vorlage bestimmt, ob das gedruckte Verzeichnis erscheint (automatisch ab zwei Einträgen, immer, nie) und bis zu welcher Stufe es reicht (nur Kapitel, bis Überschrift 3 bis 6). Ohne Verzeichnis beginnt das erste Kapitel auf Seite 1 beziehungsweise nach der Titelseite, ohne leere Seite. Lesezeichen im PDF und das Verzeichnis der Seitenansicht zeigen weiter alle Ebenen. Dokumente ohne Vorlage nutzen automatisch mit voller Tiefe. Additive Migration mit Vorgaben. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9f9370dbf8
commit
e72c9ddb8d
10 files changed
+333
-9
No files matched your search
@@ -41,6 +41,9 @@ class DocumentPdf
|
||||
|
||||
private bool $onCover = false;
|
||||
|
||||
/** Das erste Kapitel setzt die Seite fort, statt eine neue zu beginnen (ohne Inhaltsverzeichnis). */
|
||||
private bool $continueFirst = false;
|
||||
|
||||
/** Obere und untere Grenze des Textes, abhängig von den Grafiken der Vorlage. */
|
||||
private float $top = 790.0;
|
||||
|
||||
@@ -190,10 +193,21 @@ private function pass(array $guide, ?PdfTemplate $template, string $licensee, st
|
||||
$this->flow->write(PdfFlow::printable((string) $guide['title']), 24, true, 10);
|
||||
}
|
||||
|
||||
$this->flow->write('Inhaltsverzeichnis', 14, true, 8, keep: 40);
|
||||
$setting = PdfTemplate::normalizeToc($template?->toc_mode, $template?->toc_depth);
|
||||
$entries = $this->contents($guide, $setting['depth']);
|
||||
$withContents = match ($setting['mode']) {
|
||||
'never' => false,
|
||||
'always' => true,
|
||||
default => count($entries) > 1,
|
||||
};
|
||||
$this->continueFirst = ! $withContents;
|
||||
|
||||
foreach ($this->contents($guide) as $entry) {
|
||||
$this->contentsLine($entry, $known[$entry['anchor']] ?? ['page' => 1, 'top' => $this->top]);
|
||||
if ($withContents) {
|
||||
$this->flow->write('Inhaltsverzeichnis', 14, true, 8, keep: 40);
|
||||
|
||||
foreach ($entries as $entry) {
|
||||
$this->contentsLine($entry, $known[$entry['anchor']] ?? ['page' => 1, 'top' => $this->top]);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($guide['chapters'] as $chapter) {
|
||||
@@ -269,12 +283,14 @@ private function coverText(string $text, float $size, bool $bold, float &$y, flo
|
||||
|
||||
/**
|
||||
* Die Einträge des Inhaltsverzeichnisses in der Reihenfolge des Dokuments: Kapitel und Unterkapitel mit Nummer, darunter
|
||||
* ihre Zwischenüberschriften (h3 bis h6), eingerückt nach Stufe.
|
||||
* ihre Zwischenüberschriften (h3 bis h6) bis zur gewählten Tiefe, eingerückt nach Stufe. Die Lesezeichen im PDF zeigen
|
||||
* unabhängig davon alle Ebenen.
|
||||
*
|
||||
* @param array<string, mixed> $guide
|
||||
* @param int $depth höchste Überschriftenstufe im Verzeichnis (2 = nur Kapitel)
|
||||
* @return list<array{anchor: string, label: string, size: float, indent: float, bold: bool}>
|
||||
*/
|
||||
private function contents(array $guide): array
|
||||
private function contents(array $guide, int $depth = 6): array
|
||||
{
|
||||
$entries = [];
|
||||
|
||||
@@ -288,6 +304,10 @@ private function contents(array $guide): array
|
||||
];
|
||||
|
||||
foreach ($chapter['headings'] ?? [] as $heading) {
|
||||
if ($heading['level'] > $depth) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$entries[] = ['anchor' => $heading['anchor'], 'label' => $heading['text'], 'size' => 9.5, 'indent' => 18.0 + ($heading['level'] - 3) * 12.0, 'bold' => false];
|
||||
}
|
||||
}
|
||||
@@ -343,11 +363,12 @@ private function chapter(array $chapter): void
|
||||
{
|
||||
$level = $chapter['level'];
|
||||
|
||||
if ($level === 1) {
|
||||
if ($level === 1 && ! $this->continueFirst) {
|
||||
$this->closePage();
|
||||
$this->newPage();
|
||||
}
|
||||
|
||||
$this->continueFirst = false;
|
||||
$size = $level === 1 ? 18.0 : 13.5;
|
||||
$title = PdfFlow::printable($chapter['number'].' '.$chapter['title']);
|
||||
$this->flow->room(($size + 4) + 60);
|
||||
|
||||
@@ -29,6 +29,8 @@ class PdfTemplate extends Model
|
||||
'footer_align',
|
||||
'header_logo_height',
|
||||
'footer_logo_height',
|
||||
'toc_mode',
|
||||
'toc_depth',
|
||||
'publisher',
|
||||
'copyright',
|
||||
];
|
||||
@@ -49,6 +51,37 @@ protected function casts(): array
|
||||
|
||||
public const LOGO_DEFAULT = 40;
|
||||
|
||||
/** Inhaltsverzeichnis: nur bei mehr als einem Eintrag, immer oder nie. */
|
||||
public const TOC_MODES = ['auto', 'always', 'never'];
|
||||
|
||||
/** Tiefe des Verzeichnisses: 2 = nur Kapitel und Unterkapitel, 3 bis 6 = zusätzlich die Überschriften bis zu dieser Stufe. */
|
||||
public const TOC_DEPTH_MIN = 2;
|
||||
|
||||
public const TOC_DEPTH_MAX = 6;
|
||||
|
||||
/**
|
||||
* Darstellung des gedruckten Inhaltsverzeichnisses.
|
||||
*
|
||||
* @return array{mode: string, depth: int}
|
||||
*/
|
||||
public function toc(): array
|
||||
{
|
||||
return self::normalizeToc((string) $this->getAttribute('toc_mode'), (int) $this->getAttribute('toc_depth'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{mode: string, depth: int}
|
||||
*/
|
||||
public static function normalizeToc(?string $mode, int|string|null $depth): array
|
||||
{
|
||||
$depth = (int) $depth;
|
||||
|
||||
return [
|
||||
'mode' => in_array($mode, self::TOC_MODES, true) ? (string) $mode : 'auto',
|
||||
'depth' => $depth < 1 ? self::TOC_DEPTH_MAX : max(self::TOC_DEPTH_MIN, min(self::TOC_DEPTH_MAX, $depth)),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Anordnung und Logohöhe (Punkt) von Kopf oder Fuß; ungültige Werte fallen auf die Vorgabe zurück.
|
||||
*
|
||||
|
||||
@@ -44,6 +44,12 @@ class PdfVorlagen extends Page
|
||||
|
||||
public bool $showCover = true;
|
||||
|
||||
/** Inhaltsverzeichnis im PDF: `auto` (nur bei mehr als einem Eintrag), `always`, `never`. */
|
||||
public string $tocModus = 'auto';
|
||||
|
||||
/** Tiefe des Verzeichnisses: 2 = nur Kapitel, 3 bis 6 = bis zu dieser Überschriftenstufe. */
|
||||
public int $tocTiefe = PdfTemplate::TOC_DEPTH_MAX;
|
||||
|
||||
/**
|
||||
* Anordnung von Kopf und Fuß: `full` (Band in voller Seitenbreite), `left` oder `right` (Logo).
|
||||
*
|
||||
@@ -121,7 +127,7 @@ 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) !== [] || $this->layouts() !== [PdfTemplate::normalizeLayout(null, null), PdfTemplate::normalizeLayout(null, null)];
|
||||
return $this->name !== '' || $this->publisher !== '' || $this->copyright !== '' || array_filter($this->bilder) !== [] || $this->layouts() !== [PdfTemplate::normalizeLayout(null, null), PdfTemplate::normalizeLayout(null, null)] || $this->tocSetting() !== PdfTemplate::normalizeToc(null, null);
|
||||
}
|
||||
|
||||
return $this->name !== $template->name
|
||||
@@ -129,7 +135,8 @@ public function changed(): bool
|
||||
|| $this->copyright !== (string) $template->copyright
|
||||
|| $this->showCover !== (bool) $template->show_cover
|
||||
|| $this->bilder !== $this->templateSlots($template)
|
||||
|| $this->layouts() !== [$template->layout('header'), $template->layout('footer')];
|
||||
|| $this->layouts() !== [$template->layout('header'), $template->layout('footer')]
|
||||
|| $this->tocSetting() !== $template->toc();
|
||||
}
|
||||
|
||||
public function save(): void
|
||||
@@ -163,6 +170,8 @@ public function save(): void
|
||||
'header_logo_height' => $this->layouts()[0]['height'],
|
||||
'footer_align' => $this->layouts()[1]['align'],
|
||||
'footer_logo_height' => $this->layouts()[1]['height'],
|
||||
'toc_mode' => $this->tocSetting()['mode'],
|
||||
'toc_depth' => $this->tocSetting()['depth'],
|
||||
'publisher' => trim($this->publisher) === '' ? null : trim($this->publisher),
|
||||
'copyright' => trim($this->copyright) === '' ? null : trim($this->copyright),
|
||||
];
|
||||
@@ -324,6 +333,16 @@ private function loadTemplate(PdfTemplate $template): void
|
||||
$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']];
|
||||
$this->tocModus = $template->toc()['mode'];
|
||||
$this->tocTiefe = $template->toc()['depth'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{mode: string, depth: int}
|
||||
*/
|
||||
private function tocSetting(): array
|
||||
{
|
||||
return PdfTemplate::normalizeToc($this->tocModus, $this->tocTiefe);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -357,6 +376,8 @@ private function clearForm(): void
|
||||
$this->bilder = ['header' => null, 'footer' => null, 'cover' => null];
|
||||
$this->anordnung = ['header' => 'full', 'footer' => 'full'];
|
||||
$this->logohoehe = ['header' => PdfTemplate::LOGO_DEFAULT, 'footer' => PdfTemplate::LOGO_DEFAULT];
|
||||
$this->tocModus = 'auto';
|
||||
$this->tocTiefe = PdfTemplate::TOC_DEPTH_MAX;
|
||||
}
|
||||
|
||||
protected function pickerExceptId(): int
|
||||
|
||||
Reference in new issue
Block a user