Biete Tabellen und Bilder im gemeinsamen Texteditor an und setze sie in Ansicht und PDF

Tabellen und Bilder gehören zur festen Auswahl von FormattedText. Ein Bild
ist ein Verweis auf eine freigegebene Bildfassung und kommt per Picker aus
dem Medienbestand; der Stand schreibt die Bildfassung mit fest, die Rechte
entscheidet DecideAccess. Der PDF-Satz (PdfFlow) ist für Übung und kommende
Dokumente gemeinsam; PdfDocument kann zusätzlich Bilder, Flächen, Verweise
und Lesezeichen, Belege bleiben byte-gleich.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Sonnet 5.5 committed 2026-11-24 11:31:03 +01:00
1 parent 219962daff
commit c57a506f85
27 files changed
+1729 -219

No files matched your search

+21 -182
View File
@@ -3,6 +3,7 @@
namespace App\Domain\Catalog;
use App\Domain\Commerce\Receipt\PdfDocument;
use Closure;
use Transliterator;
/**
@@ -23,7 +24,7 @@ class ExercisePdf
private PdfDocument $pdf;
private float $y = self::TOP;
private PdfFlow $flow;
private int $page = 0;
@@ -64,32 +65,36 @@ public function sections(array $guide): ?array
* @param array<string, mixed> $guide
* @param list<array<string, mixed>> $sections
*/
public function render(array $guide, array $sections, string $licensee, string $copyId, string $variantName, string $guideUrl): string
public function render(array $guide, array $sections, string $licensee, string $copyId, string $variantName, string $guideUrl, ?PdfImages $images = null): string
{
$this->pdf = new PdfDocument;
$this->page = 0;
$this->footer = self::printable('Lizenziert für '.$licensee.' · Lizenzkopie '.$copyId);
$this->flow = new PdfFlow($this->pdf, self::LEFT, self::RIGHT, self::TOP, self::BOTTOM, function (): void {
$this->closePage();
$this->newPage();
}, $images === null ? null : Closure::fromCallable($images));
$this->newPage();
$this->write(self::printable((string) $guide['title']), 18, true, 8);
$this->flow->write(self::printable((string) $guide['title']), 18, true, 8);
if ($variantName !== '') {
$this->write(self::printable('Variante: '.$variantName), 11, true, 6);
$this->flow->write(self::printable('Variante: '.$variantName), 11, true, 6);
}
$this->fact('Lernziel', $guide['learning_goal']);
if ($guide['duration_minutes'] !== null) {
$this->write(self::printable('Dauer: '.$guide['duration_minutes'].($guide['duration_max'] !== null ? ' bis '.$guide['duration_max'] : '').' Minuten'), 10.5, false, 4);
$this->flow->write(self::printable('Dauer: '.$guide['duration_minutes'].($guide['duration_max'] !== null ? ' bis '.$guide['duration_max'] : '').' Minuten'), 10.5, false, 4);
}
if (filled($guide['prerequisites'])) {
$this->write(self::printable('Voraussetzungen: '.$guide['prerequisites']), 10.5, false, 4);
$this->flow->write(self::printable('Voraussetzungen: '.$guide['prerequisites']), 10.5, false, 4);
}
$this->fact('Ausstattung', $guide['material_note']);
$this->y -= 6;
$this->flow->y -= 6;
foreach ($sections as $section) {
$this->section($section, $guideUrl);
@@ -108,16 +113,7 @@ public function render(array $guide, array $sections, string $licensee, string $
*/
public static function printable(string $text): string
{
$text = strtr($text, ['→' => '->', '←' => '<-', '≥' => '>=', '≤' => '<=', '✓' => '+', '✗' => 'x', '≠' => '!=', '≈' => '~']);
$latin = Transliterator::create('Any-Latin; Latin-ASCII');
$out = '';
foreach (mb_str_split($text) as $char) {
$encoded = mb_convert_encoding($char, 'Windows-1252', 'UTF-8');
$out .= $encoded === '?' && $char !== '?' ? ($latin?->transliterate($char) ?: '?') : $char;
}
return $out;
return PdfFlow::printable($text);
}
/**
@@ -131,16 +127,16 @@ private function section(array $section, string $guideUrl): void
$label = in_array($section['class'], ['film', 'audio'], true)
? 'Film: '.$title.' - online abrufbar im Mitgliederbereich (geschützte Wiedergabe): '.$guideUrl
: 'Material: '.$title.' - im Mitgliederbereich abrufbar: '.$guideUrl;
$this->write(self::printable($label), 10, false, 6);
$this->flow->write(self::printable($label), 10, false, 6);
return;
}
$this->write($title, 12.5, true, 5);
$this->flow->write($title, 12.5, true, 5, keep: 40);
$this->formatted($section['blocks'] ?? []);
$this->flow->formatted($section['blocks'] ?? []);
$this->y -= 4;
$this->flow->y -= 4;
}
/** Angabe mit Bezeichnung; formatierte Angaben stehen unter der Bezeichnung, einfache dahinter. */
@@ -153,177 +149,20 @@ private function fact(string $label, ?string $value): void
}
if (count($blocks) === 1 && $blocks[0]['type'] === 'paragraph') {
$this->runs([['text' => $label.': ', 'bold' => true, 'italic' => false, 'href' => null], ...$blocks[0]['runs']], self::LEFT, 10.5, after: 4);
$this->flow->runs([['text' => $label.': ', 'bold' => true, 'italic' => false, 'href' => null], ...$blocks[0]['runs']], self::LEFT, 10.5, after: 4);
return;
}
$this->write(self::printable($label.':'), 10.5, true, 2);
$this->formatted($blocks);
}
/**
* Blöcke aus {@see FormattedText::blocks()}: Absatz, Zwischenüberschrift, Zitat und Listen mit fett und kursiv.
*
* @param list<array<string, mixed>> $blocks
*/
private function formatted(array $blocks): void
{
foreach ($blocks as $block) {
match ($block['type']) {
'heading' => $this->runs($block['runs'], self::LEFT, 11, bold: true, after: 4),
'quote' => $this->runs($block['runs'], self::LEFT + 14, 10.5, italic: true, after: 5),
'bullets' => $this->listItems($block['items'], fn (int $n): string => '•'),
'numbered' => $this->listItems($block['items'], fn (int $n): string => $n.'.'),
default => $this->runs($block['runs'] ?? [], self::LEFT, 10.5, after: 5),
};
}
}
/**
* @param list<list<array{text: string, bold: bool, italic: bool, href: ?string}>> $items
*/
private function listItems(array $items, callable $marker): void
{
foreach ($items as $index => $item) {
$this->room(14);
$this->pdf->text(self::LEFT + 4, $this->y, $marker($index + 1), 10.5);
$this->runs($item, self::LEFT + 18, 10.5, after: 1);
}
$this->y -= 3;
}
/**
* Setzt Textabschnitte mit fett und kursiv in Zeilen um. Die Breite kommt aus den Schriftbreiten der Helvetica
* ({@see PdfDocument::exactWidth()}), damit verschiedene Schnitte in einer Zeile ohne Lücken aneinanderstoßen.
* Links erscheinen mit ausgeschriebener Adresse, weil sie auf Papier sonst verloren gehen.
*
* @param list<array{text: string, bold: bool, italic: bool, href: ?string}> $runs
*/
private function runs(array $runs, float $x, float $size, bool $bold = false, bool $italic = false, float $after = 0): void
{
$maxWidth = self::RIGHT - $x;
$lines = [];
$line = [];
$width = 0.0;
$pendingSpace = false;
$count = count($runs);
$flush = function () use (&$lines, &$line, &$width, &$pendingSpace): void {
if ($line !== []) {
$lines[] = $line;
}
$line = [];
$width = 0.0;
$pendingSpace = false;
};
$place = function (string $word, bool $isBold, bool $isItalic) use (&$line, &$width, &$pendingSpace, $maxWidth, $size, $flush): void {
$space = $pendingSpace && $line !== [] ? ' ' : '';
$need = PdfDocument::exactWidth($space.$word, $size, $isBold);
if ($line !== [] && $width + $need > $maxWidth) {
$flush();
$space = '';
$need = PdfDocument::exactWidth($word, $size, $isBold);
}
while ($need > $maxWidth && mb_strlen($word) > 1) {
$cut = mb_strlen($word) - 1;
while ($cut > 1 && PdfDocument::exactWidth(mb_substr($word, 0, $cut), $size, $isBold) > $maxWidth) {
$cut--;
}
$line[] = [mb_substr($word, 0, $cut), $isBold, $isItalic];
$flush();
$word = mb_substr($word, $cut);
$need = PdfDocument::exactWidth($word, $size, $isBold);
}
$last = $line === [] ? null : $line[array_key_last($line)];
if ($last !== null && $last[1] === $isBold && $last[2] === $isItalic) {
$line[array_key_last($line)][0] .= $space.$word;
} else {
$line[] = [$space.$word, $isBold, $isItalic];
}
$width += $need;
$pendingSpace = false;
};
foreach ($runs as $index => $run) {
$isBold = $bold || $run['bold'];
$isItalic = $italic || $run['italic'];
foreach (preg_split('/(\s+)/u', self::printable($run['text']), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY) ?: [] as $token) {
if (trim($token) !== '') {
$place($token, $isBold, $isItalic);
continue;
}
if (str_contains($token, "\n")) {
$flush();
continue;
}
$pendingSpace = true;
}
$nextHref = $index + 1 < $count ? $runs[$index + 1]['href'] : null;
if ($run['href'] !== null && $nextHref !== $run['href'] && ! str_contains($run['text'], $run['href'])) {
$pendingSpace = true;
$place('('.self::printable($run['href']).')', false, false);
}
}
$flush();
foreach ($lines as $row) {
$this->room($size + 4);
$cursor = $x;
foreach ($row as [$text, $isBold, $isItalic]) {
$this->pdf->text($cursor, $this->y, $text, $size, $isBold, $isItalic);
$cursor += PdfDocument::exactWidth($text, $size, $isBold);
}
$this->y -= $size + 4;
}
$this->y -= $after;
}
private function write(string $text, float $size, bool $bold, float $after): void
{
foreach (PdfDocument::wrap($text, self::RIGHT - self::LEFT, $size, $bold) as $line) {
$this->room($size + 4);
$this->pdf->text(self::LEFT, $this->y, $line, $size, $bold);
$this->y -= $size + 4;
}
$this->y -= $after;
}
private function room(float $needed): void
{
if ($this->y - $needed < self::BOTTOM) {
$this->closePage();
$this->newPage();
}
$this->flow->write(self::printable($label.':'), 10.5, true, 2);
$this->flow->formatted($blocks);
}
private function newPage(): void
{
$this->pdf->addPage();
$this->page++;
$this->y = self::TOP;
$this->flow->restart();
}
private function closePage(): void