Mache Links im PDF anklickbar und trenne Anzeige und Adresse
Deploy Test / test (push) Successful in 4m57s
Deploy Test / deploy (push) Successful in 34s

Externe Adressen standen als reiner Text im PDF; der PDF-Betrachter erkennt sie
selbst und rät bei einer am Zeilenende umbrochenen Adresse die Fortsetzung falsch.
Jetzt ist jeder Link ein echter Verweis: Die vollständige Adresse hängt an jedem
Zeilenstück des Linktexts und bleibt über Umbrüche zusammen; der Link ist
unterstrichen, auch in Tabellen und Listen. Gedruckt steht die Adresse nur in
Kurzform (ohne Schema, höchstens 42 Zeichen), damit kein Betrachter sie als Text
erraten muss. Das ersetzt die ausgeschriebene Adresse in Klammern; der Test der
Übungsausgabe ist bewusst angepasst.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Sonnet 5.5 committed 2026-11-24 21:27:33 +01:00
1 parent 580b27451c
commit 18ee22f37e
7 files changed
+159 -37

No files matched your search

+36 -10
View File
@@ -33,7 +33,7 @@ final class PdfDocument
/** @var array<int, array<int, true>> Seite => Bilder, die sie zeigt */
private array $pageImages = [];
/** @var array<int, list<array{x: float, y: float, w: float, h: float, page: int, top: float}>> */
/** @var array<int, list<array{x: float, y: float, w: float, h: float, page?: int, top?: float, uri?: string}>> */
private array $links = [];
/** @var list<array{title: string, page: int, top: float, level: int}> */
@@ -98,6 +98,32 @@ public function link(float $x, float $y, float $w, float $h, int $page, float $t
$this->links[array_key_last($this->pages)][] = ['x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'page' => $page, 'top' => $top];
}
/**
* Anklickbarer Bereich auf der aktuellen Seite, der eine Internetadresse öffnet. Eine Adresse, die am Zeilenende umbricht,
* bekommt für jede Zeile einen Bereich mit derselben vollständigen Adresse: Sie bleibt als Ganzes zusammen, gleich wie
* der sichtbare Text umbricht.
*/
public function uriLink(float $x, float $y, float $w, float $h, string $uri): void
{
if ($this->pages === []) {
$this->addPage();
}
$this->links[array_key_last($this->pages)][] = ['x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'uri' => $uri];
}
/**
* Die Adresse als 7-Bit-Zeichenkette für das PDF: Zeichen außerhalb von ASCII und Leerzeichen werden prozentkodiert,
* unsichtbare Zeichen entfallen.
*/
private static function uriString(string $uri): string
{
$uri = (string) preg_replace('/[\x{00AD}\x{034F}\x{061C}\x{180E}\x{200B}-\x{200F}\x{202A}-\x{202E}\x{2060}-\x{2064}\x{FEFF}]/u', '', $uri);
$uri = (string) preg_replace_callback('/[^\x21-\x7E]/', fn (array $m): string => rawurlencode($m[0]), $uri);
return str_replace(['\\', '(', ')'], ['\\\\', '\\(', '\\)'], $uri);
}
/** Lesezeichen (Ebene 1 bis 6) für die Gliederung in PDF-Betrachtern. */
public function bookmark(string $title, int $page, float $top, int $level = 1): void
{
@@ -266,15 +292,15 @@ public function output(string $title, ?string $keywords = null): string
foreach ($annotations as $links) {
foreach ($links as $link) {
$objects[$link['number']] = sprintf(
'<< /Type /Annot /Subtype /Link /Rect [%s %s %s %s] /Border [0 0 0] /Dest [%d 0 R /XYZ 0 %s null] >>',
$this->n($link['x']),
$this->n($link['y']),
$this->n($link['x'] + $link['w']),
$this->n($link['y'] + $link['h']),
6 + 2 * min($link['page'], count($pages) - 1),
$this->n($link['top']),
);
$rect = sprintf('[%s %s %s %s]', $this->n($link['x']), $this->n($link['y']), $this->n($link['x'] + $link['w']), $this->n($link['y'] + $link['h']));
$objects[$link['number']] = isset($link['uri'])
? '<< /Type /Annot /Subtype /Link /Rect '.$rect.' /Border [0 0 0] /A << /S /URI /URI ('.self::uriString($link['uri']).') >> >>'
: sprintf(
'<< /Type /Annot /Subtype /Link /Rect %s /Border [0 0 0] /Dest [%d 0 R /XYZ 0 %s null] >>',
$rect,
6 + 2 * min($link['page'], count($pages) - 1),
$this->n($link['top']),
);
}
}