Mache Anzeige und Tiefe des PDF-Inhaltsverzeichnisses in der Vorlage einstellbar
Deploy Test / test (push) Successful in 3m20s
Deploy Test / deploy (push) Successful in 24s

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:
LarsandClaude Sonnet 5.5 committed 2026-11-24 16:54:51 +01:00
1 parent 9f9370dbf8
commit e72c9ddb8d
10 files changed
+333 -9

No files matched your search

+27 -6
View File
@@ -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);