Files
laraundfelix/app/Domain/Catalog/ExercisePdf.php
T
LarsandClaude Sonnet 5.5 c57a506f85 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>
2026-11-24 11:31:03 +01:00

175 lines
5.7 KiB
PHP

<?php
namespace App\Domain\Catalog;
use App\Domain\Commerce\Receipt\PdfDocument;
use Closure;
use Transliterator;
/**
* Druckbare Ausgabe einer Übung (DIN A4, mehrere Seiten) aus demselben Ergebnis wie die Ansicht ({@see ExerciseGuide}).
* Kennzeichnung: Fußzeile mit Lizenznehmer und Lizenzkopie auf jeder Seite (K2) und die Kennung in den Metadaten (K1).
* Das Dokument wird bei jedem Abruf neu erzeugt und nie abgelegt, damit eine spätere Sperre keine ältere Kopie
* ausliefern kann.
*/
class ExercisePdf
{
private const LEFT = 56.0;
private const RIGHT = 539.0;
private const TOP = 790.0;
private const BOTTOM = 72.0;
private PdfDocument $pdf;
private PdfFlow $flow;
private int $page = 0;
private string $footer = '';
/**
* Welche Abschnitte gehören ins PDF? Erforderliche Textabschnitte der Auswahl müssen für `laden` zulässig sein, sonst
* ist das PDF nicht erlaubt (null). Ergänzende ohne Berechtigung entfallen. Videos und Dokumente erscheinen nur als
* Hinweis.
*
* @param array<string, mixed> $guide Ergebnis von {@see ExerciseGuide::build}
* @return list<array<string, mixed>>|null
*/
public function sections(array $guide): ?array
{
$included = [];
foreach ($guide['entries'] as $entry) {
if ($entry['class'] === 'text') {
if ($entry['may_download'] && $entry['allowed']) {
$included[] = $entry;
} elseif ($entry['requirement'] === CompositionMember::REQUIRED) {
return null;
}
continue;
}
if ($entry['allowed']) {
$included[] = $entry;
}
}
return array_filter($included, fn (array $entry): bool => $entry['class'] === 'text') === [] ? null : $included;
}
/**
* @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, ?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->flow->write(self::printable((string) $guide['title']), 18, true, 8);
if ($variantName !== '') {
$this->flow->write(self::printable('Variante: '.$variantName), 11, true, 6);
}
$this->fact('Lernziel', $guide['learning_goal']);
if ($guide['duration_minutes'] !== null) {
$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->flow->write(self::printable('Voraussetzungen: '.$guide['prerequisites']), 10.5, false, 4);
}
$this->fact('Ausstattung', $guide['material_note']);
$this->flow->y -= 6;
foreach ($sections as $section) {
$this->section($section, $guideUrl);
}
$this->closePage();
$ascii = Transliterator::create('Any-Latin; Latin-ASCII');
return $this->pdf->output($ascii?->transliterate((string) $guide['title']) ?: 'Uebung', 'Lizenzkopie '.$copyId);
}
/**
* Zeichen außerhalb von Windows-1252 werden, wo möglich, lesbar ersetzt (Pfeil, Ungleichheitszeichen, andere
* lateinische Buchstaben); sonst bleibt „?“.
*/
public static function printable(string $text): string
{
return PdfFlow::printable($text);
}
/**
* @param array<string, mixed> $section
*/
private function section(array $section, string $guideUrl): void
{
$title = self::printable((string) $section['title']);
if ($section['class'] !== 'text') {
$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->flow->write(self::printable($label), 10, false, 6);
return;
}
$this->flow->write($title, 12.5, true, 5, keep: 40);
$this->flow->formatted($section['blocks'] ?? []);
$this->flow->y -= 4;
}
/** Angabe mit Bezeichnung; formatierte Angaben stehen unter der Bezeichnung, einfache dahinter. */
private function fact(string $label, ?string $value): void
{
$blocks = FormattedText::blocks($value);
if ($blocks === []) {
return;
}
if (count($blocks) === 1 && $blocks[0]['type'] === 'paragraph') {
$this->flow->runs([['text' => $label.': ', 'bold' => true, 'italic' => false, 'href' => null], ...$blocks[0]['runs']], self::LEFT, 10.5, after: 4);
return;
}
$this->flow->write(self::printable($label.':'), 10.5, true, 2);
$this->flow->formatted($blocks);
}
private function newPage(): void
{
$this->pdf->addPage();
$this->page++;
$this->flow->restart();
}
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, 8);
}
}