*/ private array $pages = []; public function addPage(): void { $this->pages[] = ''; } public function text(float $x, float $y, string $text, float $size = 10, bool $bold = false): void { $this->append(sprintf("BT /%s %s Tf %s %s Td (%s) Tj ET\n", $bold ? 'F2' : 'F1', $this->n($size), $this->n($x), $this->n($y), self::escape($text))); } public function textRight(float $xRight, float $y, string $text, float $size = 10, bool $bold = false): void { $this->text($xRight - self::width($text, $size, $bold), $y, $text, $size, $bold); } public function line(float $x1, float $y1, float $x2, float $y2, float $width = 0.5): void { $this->append(sprintf("%s w %s %s m %s %s l S\n", $this->n($width), $this->n($x1), $this->n($y1), $this->n($x2), $this->n($y2))); } /** * Zeilenumbruch nach Wörtern. Die Breite ist eine bewusst großzügige Schätzung der Helvetica-Breiten, damit nichts * über den Rand läuft; ein einzelnes zu langes Wort wird hart getrennt. * * @return list */ public static function wrap(string $text, float $maxWidth, float $size = 10, bool $bold = false): array { $lines = []; foreach (preg_split('/\R/u', trim($text)) ?: [''] as $paragraph) { $current = ''; foreach (preg_split('/\s+/u', trim($paragraph)) ?: [] as $word) { while (mb_strlen($word) > 1 && self::width($word, $size, $bold) > $maxWidth) { $cut = mb_strlen($word) - 1; while ($cut > 1 && self::width(mb_substr($word, 0, $cut), $size, $bold) > $maxWidth) { $cut--; } if ($current !== '') { $lines[] = $current; $current = ''; } $lines[] = mb_substr($word, 0, $cut); $word = mb_substr($word, $cut); } $candidate = $current === '' ? $word : $current.' '.$word; if ($current !== '' && self::width($candidate, $size, $bold) > $maxWidth) { $lines[] = $current; $current = $word; } else { $current = $candidate; } } $lines[] = $current; } return $lines; } /** Geschätzte Textbreite in Punkt (Helvetica; Ziffern und Währung exakt, Buchstaben großzügig). */ public static function width(string $text, float $size = 10, bool $bold = false): float { $units = 0.0; foreach (mb_str_split($text) as $char) { $width = match (true) { $char === ' ', $char === ',', $char === '.', $char === ':', $char === ';', $char === "'", $char === '|', $char === 'i', $char === 'l', $char === 'I', $char === 'j', $char === 't', $char === 'f' => 0.3, $char === '-', $char === '(', $char === ')', $char === '/', $char === 'r' => 0.35, ctype_digit($char), $char === '€' => 0.556, mb_strtoupper($char) === $char && $char !== mb_strtolower($char) => 0.75, $char === 'm', $char === 'w', $char === 'M', $char === 'W' => 0.85, default => 0.57, }; // Fette Ziffern, Währung und Satzzeichen sind in Helvetica so breit wie normale; nur Buchstaben werden breiter. $units += $bold && ctype_alpha($char) ? $width * 1.08 : $width; } return $units * $size; } /** * @param string $title Nur ASCII (Dokumenttitel) */ public function output(string $title): string { $pages = $this->pages === [] ? [''] : $this->pages; $objects = []; $objects[1] = '<< /Type /Catalog /Pages 2 0 R >>'; $kids = []; $next = 6; foreach ($pages as $stream) { $kids[] = $next.' 0 R'; $objects[$next] = '<< /Type /Page /Parent 2 0 R /MediaBox [0 0 '.self::WIDTH.' '.self::HEIGHT.'] /Resources << /Font << /F1 3 0 R /F2 4 0 R >> >> /Contents '.($next + 1).' 0 R >>'; $objects[$next + 1] = '<< /Length '.strlen($stream)." >>\nstream\n".$stream.'endstream'; $next += 2; } $objects[2] = '<< /Type /Pages /Kids ['.implode(' ', $kids).'] /Count '.count($pages).' >>'; $objects[3] = '<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding >>'; $objects[4] = '<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica-Bold /Encoding /WinAnsiEncoding >>'; $objects[5] = '<< /Title ('.preg_replace('/[^\x20-\x7E]/', '?', str_replace(['\\', '(', ')'], ['', '', ''], $title)).') /Producer (Lara und Felix) >>'; ksort($objects); $pdf = "%PDF-1.4\n%\xE2\xE3\xCF\xD3\n"; $offsets = []; foreach ($objects as $number => $body) { $offsets[$number] = strlen($pdf); $pdf .= $number." 0 obj\n".$body."\nendobj\n"; } $xref = strlen($pdf); $pdf .= 'xref'."\n0 ".(count($objects) + 1)."\n0000000000 65535 f \n"; foreach ($offsets as $offset) { $pdf .= sprintf("%010d 00000 n \n", $offset); } return $pdf.'trailer'."\n<< /Size ".(count($objects) + 1).' /Root 1 0 R /Info 5 0 R >>'."\nstartxref\n".$xref."\n%%EOF\n"; } private function append(string $operators): void { if ($this->pages === []) { $this->addPage(); } $this->pages[array_key_last($this->pages)] .= $operators; } private function n(float $value): string { return rtrim(rtrim(number_format($value, 2, '.', ''), '0'), '.') ?: '0'; } /** Windows-1252, geklammert für PDF-Zeichenketten. */ private static function escape(string $text): string { $text = strtr($text, ["\u{2212}" => '-', "\u{2019}" => "'", "\u{2026}" => '...', "\u{00A0}" => ' ', "\u{202F}" => ' ']); $text = (string) preg_replace('/[\x00-\x08\x0B-\x1F\x7F]/u', '', $text); $encoded = mb_convert_encoding($text, 'Windows-1252', 'UTF-8'); return str_replace(['\\', '(', ')'], ['\\\\', '\\(', '\\)'], $encoded); } }