Führe Dokumente mit Kapiteln, Fassung, Seite und PDF mit Inhaltsverzeichnis ein (ADR-51)

Ein Dokument ist eine Komposition der Art document, ein Kapitel ein Text mit der
automatischen Materialart Dokumentkapitel; die Ebene steht in der Rolle. Der
Aufbaueditor bekommt den Kapitelarbeitsplatz (Übersicht, Ordnen als Block,
offene Eingaben bleiben beim Wechsel erhalten, Speichern als Entwurf,
Bereitstellen als Dokumentfassung). Module und Programme binden eine Fassung
über den gemeinsamen Picker ein. Mitglieder lesen sie mit anklickbarem
Inhaltsverzeichnis und laden ein PDF mit Inhaltsverzeichnis, Seitenzahlen,
Tabellen und Bildern. Ein älterer Stand liefert die gebundene Fassung weiter.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Sonnet 5.5 committed 2026-11-24 12:23:18 +01:00
1 parent c57a506f85
commit 3e2fd872c2
43 files changed
+2827 -64

No files matched your search

+15 -2
View File
@@ -127,12 +127,25 @@ public function test_an_image_is_a_reference_to_an_image_version_and_never_an_ad
{
$html = FormattedText::canonical('<p>Davor</p><img data-id="12" alt="Ein Bild" src="https://evil.example/x.png" onerror="x()" width="40"><p>Danach</p><img src="y.png"><p>Eingebettet <img data-id="9" alt="x"> im Absatz</p>');
$this->assertSame('<p>Davor</p><img data-id="12" alt="Ein Bild"><p>Danach</p><p>Eingebettet im Absatz</p>', $html);
$this->assertSame([12], FormattedText::imageIds($html));
$this->assertSame('<p>Davor</p><img data-id="12" alt="Ein Bild"><p>Danach</p><p>Eingebettet <img data-id="9" alt="x"> im Absatz</p>', $html, 'Der Editor setzt ein Bild mitten in einen Absatz; es bleibt dort.');
$this->assertSame([12, 9], FormattedText::imageIds($html));
$this->assertSame([['type' => 'paragraph', 'text' => 'Eingebettet'], ['type' => 'image', 'id' => 9], ['type' => 'paragraph', 'text' => 'im Absatz']], array_map(fn (array $block): array => $block['type'] === 'image' ? ['type' => 'image', 'id' => $block['id']] : ['type' => $block['type'], 'text' => trim(implode('', array_column($block['runs'], 'text')))], array_slice(FormattedText::blocks($html), 3)), 'Im PDF teilt das Bild den Absatz.');
$this->assertSame('<h3>Kopf</h3><ul><li><p>Punkt</p></li></ul>', FormattedText::canonical('<h3>Kopf<img data-id="5"></h3><ul><li><p>Punkt<img data-id="6"></p></li></ul>'), 'In Überschrift und Liste entfällt ein Bild.');
$this->assertSame($html, FormattedText::canonical($html));
$this->assertSame('<img data-id="3" alt="">', FormattedText::canonical('<img data-id="3" alt="">'), 'Ein Text nur aus einem Bild ist nicht leer.');
}
public function test_an_image_in_a_table_cell_is_kept_because_the_editor_can_put_it_there(): void
{
$html = FormattedText::canonical('<table><tbody><tr><td><p>Stuhlkreis</p><img data-id="7" alt="Aufstellung" src="x.png"></td></tr></tbody></table>');
$this->assertSame('<table><tbody><tr><td><p>Stuhlkreis</p><img data-id="7" alt="Aufstellung"></td></tr></tbody></table>', $html);
$this->assertSame([7], FormattedText::imageIds($html));
$this->assertSame('image', FormattedText::blocks($html)[0]['rows'][0][0]['blocks'][1]['type']);
$this->assertSame($html, FormattedText::canonical($html));
$this->assertSame('<table><tbody><tr><td><img data-id="7" alt=""></td></tr></tbody></table>', FormattedText::canonical('<table><tbody><tr><td><img data-id="7"></td></tr></tbody></table>'), 'Eine Zelle nur mit Bild bleibt erhalten.');
}
public function test_images_appear_only_where_the_reader_resolves_them(): void
{
$html = '<p>Davor</p><img data-id="12" alt="Ein Bild"><img data-id="13" alt="Anderes"><p>Danach</p>';
+103
View File
@@ -0,0 +1,103 @@
<?php
namespace Tests\Unit;
use App\Domain\Catalog\DocumentOutline;
use App\Domain\Commerce\Receipt\PdfDocument;
use App\Domain\Media\PdfImage;
use Tests\TestCase;
class PdfDocumentTest extends TestCase
{
public function test_a_document_without_the_new_features_keeps_its_exact_bytes(): void
{
// Prüfsummen aus dem Stand vor ADR-51: Belege und Übungs-PDFs ändern sich durch den erweiterten Schreiber nicht.
$pdf = new PdfDocument;
$pdf->addPage();
$pdf->text(56, 780, 'Überschrift mit Umlauten äöü ß', 18, true);
$pdf->text(56, 760, 'Normal und kursiv', 10.5, false, true);
$pdf->text(56, 740, 'Fett kursiv', 10.5, true, true);
$pdf->line(56, 54, 539, 54, 0.4);
$pdf->textRight(539, 40, 'Seite 1', 8);
$pdf->addPage();
$pdf->text(56, 780, 'Zweite Seite (Klammern) \\ Rest', 11);
$this->assertSame('a2b11d725eecc67e7cbc07a98136a3a7', md5($pdf->output('Titel', 'Lizenzkopie X')));
$small = new PdfDocument;
$small->addPage();
$small->text(10, 10, 'x');
$this->assertSame('2c520ed928e812821fa8fac19d236d88', md5($small->output('T')));
}
public function test_links_bookmarks_images_and_areas_form_a_readable_structure(): void
{
$image = PdfImage::fromBytes($this->png(300, 200));
$this->assertNotNull($image);
$pdf = new PdfDocument;
$pdf->addPage();
$pdf->text(56, 780, 'Inhalt', 14, true);
$pdf->link(56, 770, 200, 14, 1, 790);
$pdf->fillRect(56, 700, 100, 20, 0.9);
$pdf->addPage();
$pdf->bookmark('1 Kapitel', 1, 790, 1);
$pdf->bookmark('1.1 Unterkapitel (mit) Klammern', 1, 700, 2);
$pdf->bookmark('2 Kapitel', 1, 400, 1);
$pdf->image(56, 500, 200, 133, $image['jpeg'], $image['width'], $image['height']);
$pdf->image(300, 500, 200, 133, $image['jpeg'], $image['width'], $image['height']);
$out = $pdf->output('Probe');
$this->assertSame(1, substr_count($out, '/Subtype /Image'), 'Dasselbe Bild liegt nur einmal im Dokument.');
$this->assertStringContainsString('/Annots [', $out);
$this->assertStringContainsString('/Dest [8 0 R /XYZ 0 790 null]', $out, 'Das Ziel ist die zweite Seite.');
$this->assertStringContainsString('/Outlines', $out);
$this->assertStringContainsString('/PageMode /UseOutlines', $out);
$this->assertSame(4, substr_count($out, '/Title ('), 'Dokumenttitel und drei Lesezeichen.');
$this->assertStringContainsString('1.1 Unterkapitel \\(mit\\) Klammern', $out);
$this->assertSame(1, preg_match('/\/Count 3 >>/', $out), 'Die Gliederung zählt alle Einträge.');
// Jede Objektnummer der Kreuzverweistabelle zeigt auf ein Objekt.
preg_match('/startxref\n(\d+)/', $out, $start);
$xref = substr($out, (int) $start[1]);
preg_match_all('/^(\d{10}) 00000 n $/m', $xref, $offsets);
foreach ($offsets[1] as $number => $offset) {
$this->assertStringStartsWith(($number + 1).' 0 obj', substr($out, (int) $offset, 12), 'Objekt '.($number + 1));
}
}
public function test_images_are_reencoded_as_jpeg_scaled_and_flattened_on_white(): void
{
$large = PdfImage::fromBytes($this->png(3000, 1000));
$this->assertSame(PdfImage::MAX_EDGE, $large['width']);
$this->assertSame(467, $large['height']);
$this->assertSame("\xFF\xD8", substr($large['jpeg'], 0, 2));
$this->assertNull(PdfImage::fromBytes('kein Bild'));
$this->assertNull(PdfImage::fromBytes(''));
}
public function test_the_outline_numbers_chapters_and_subchapters_from_their_order(): void
{
$numbering = DocumentOutline::numbering(['Kapitel', 'Unterkapitel', 'Unterkapitel', 'Kapitel', 'Unterkapitel']);
$this->assertSame(['1', '1.1', '1.2', '2', '2.1'], array_column($numbering, 'number'));
$this->assertSame(['kapitel-1', 'kapitel-1-1', 'kapitel-1-2', 'kapitel-2', 'kapitel-2-1'], array_column($numbering, 'anchor'));
$this->assertSame(['1', '2'], array_column(DocumentOutline::numbering(['Unterkapitel', 'Kapitel']), 'number'), 'Ein Unterkapitel ohne Kapitel davor zählt als Kapitel.');
$this->assertSame(2, DocumentOutline::blockEnd(['Kapitel', 'Unterkapitel', 'Unterkapitel', 'Kapitel'], 0));
$this->assertSame(1, DocumentOutline::blockEnd(['Kapitel', 'Unterkapitel', 'Unterkapitel', 'Kapitel'], 1));
$this->assertSame(3, DocumentOutline::blockEnd(['Kapitel', 'Unterkapitel', 'Unterkapitel', 'Kapitel'], 3));
}
private function png(int $width, int $height): string
{
$image = imagecreatetruecolor($width, $height);
imagefill($image, 0, 0, (int) imagecolorallocate($image, 30, 90, 70));
ob_start();
imagepng($image);
return (string) ob_get_clean();
}
}