fix(documents): Logo im PDF und umbrochene Namen im Vertragskopf

- app/pdf.php: PdfDoc::image() kann jetzt auch PNG (8 Bit, ohne Palette,
  nicht interlaced, inkl. Alphakanal über /SMask); PNG-Zeilenfilter 0–4 und
  Paeth werden rückgängig gemacht, RGB und Alpha als FlateDecode eingebettet.
  JPEG-Weg (Unterschrift) unverändert.
- app/documents.php: document_pdf_title() setzt das Drink-it-cold-Logo oben
  rechts (web/assets/img/logo-drink-it-cold-480.png, Fallback 720er-Version).
- app/documents.php: document_pdf_parties() bricht Mieter- und Vermietername
  auf die jeweilige Spaltenbreite um; Adressblock und Rückgabewert richten
  sich an der längeren Namenszeile aus, nichts läuft mehr in die Nachbarspalte.
This commit is contained in:
torsten 2026-11-30 16:29:46 +01:00
parent 8f45f756c0
commit a755e7af81
2 changed files with 223 additions and 22 deletions

View file

@ -298,10 +298,26 @@ function document_pdf_footer(PdfDoc $pdf, string $label): void
$pdf->text(50, $h - 38, 7.5, 'Seite ' . $pdf->pageNo(), false, 'right', $w - 100);
}
/** Kopfzeile mit Titel und rechtsstehender Kennung. */
/** Logo-Datei für die PDF-Kopfzeile (PNG mit Transparenz) oder null. */
function document_logo_path(): ?string
{
foreach (['/web/assets/img/logo-drink-it-cold-480.png', '/web/assets/img/logo-drink-it-cold.png'] as $candidate) {
if (is_file(DIC_ROOT . $candidate)) {
return DIC_ROOT . $candidate;
}
}
return null;
}
/** Kopfzeile: Logo oben rechts, Titel links, Kennung rechts über der Linie. */
function document_pdf_title(PdfDoc $pdf, string $title, string $right): void
{
$w = $pdf->pageWidth();
$logo = document_logo_path();
if ($logo !== null) {
// Seitenverhältnis des Logos 480:267 – Breite 54 pt, Höhe passend.
$pdf->image($logo, $w - 50 - 54.0, 22.0, 54.0, 30.0);
}
$pdf->text(50, 66, 20, $title, true);
$pdf->text(50, 66, 8.5, $right, false, 'right', $w - 100);
$pdf->line(50, 76, $w - 50, 76, 1.0);
@ -320,8 +336,16 @@ function document_pdf_parties(PdfDoc $pdf, float $top, array $booking): float
$pdf->text($right, $top, 7.5, 'VERMIETER', true);
$y = $top + 14;
$pdf->text($left, $y, 11, (string) $booking['customer_name'], true);
$pdf->text($right, $y, 11, $owner, true);
// Lange Firmennamen umbrechen, damit sie nicht in die rechte Spalte laufen.
$nameLines = $pdf->wrap((string) $booking['customer_name'], 11, $right - $left - 24, true);
$ownerLines = $pdf->wrap($owner, 11, $pdf->pageWidth() - 50 - $right, true);
foreach ($nameLines as $i => $line) {
$pdf->text($left, $y + $i * 13, 11, $line, true);
}
foreach ($ownerLines as $i => $line) {
$pdf->text($right, $y + $i * 13, 11, $line, true);
}
$y += (max(count($nameLines), count($ownerLines)) - 1) * 13;
$pdf->text($right, $y + 13, 9.5, (string) $legal['business_name']);
$customer = array_values(array_filter([

View file

@ -6,8 +6,9 @@ declare(strict_types=1);
*
* Auf dem Hosting steht keine Composer-Bibliothek (mPDF/TCPDF) zur Verfügung,
* deshalb erzeugt diese Klasse PDF 1.4 selbst: Helvetica als Kernschrift,
* Text in CP1252 (deckt deutsche Umlaute ab), Linien, Rechtecke und eingebettete
* JPEG-Bilder (z. B. die Unterschrift vom Tablet).
* Text in CP1252 (deckt deutsche Umlaute ab), Linien, Rechtecke sowie
* eingebettete Bilder – JPEG (z. B. die Unterschrift vom Tablet) und PNG
* (Logo, inklusive Transparenz über /SMask).
*
* Koordinaten werden in Punkten von links oben angegeben; die Umrechnung ins
* PDF-Koordinatensystem (Ursprung links unten) passiert intern.
@ -42,7 +43,7 @@ final class PdfDoc
/** @var list<string> */
private array $pages = [];
private string $stream = '';
/** @var array<string, array{data: string, w: int, h: int, gray: bool}> */
/** @var array<string, array{data: string, w: int, h: int, gray: bool, filter: string, smask: ?string}> */
private array $images = [];
public function __construct(float $width = self::A4_W, float $height = self::A4_H)
@ -190,24 +191,60 @@ final class PdfDoc
: sprintf("%.2F w %.2F %.2F %.2F %.2F re S\n", $lw, $x, $y, $w, $h);
}
/** Bettet ein JPEG ein (für Unterschriften aus dem Browser-Canvas). */
/**
* Bettet ein Bild ein: JPEG (Unterschrift vom Tablet) oder PNG (Logo,
* auch mit Alphakanal). Liefert false, wenn das Format nicht unterstützt wird.
*/
public function image(string $file, float $x, float $top, float $w, float $h): bool
{
$data = @file_get_contents($file);
if (!is_string($data) || strncmp($data, "\xFF\xD8", 2) !== 0) {
return false;
}
$size = self::jpeg_size($data);
if ($size === null) {
if (!is_string($data)) {
return false;
}
$name = 'Im' . (count($this->images) + 1);
$this->images[$name] = [
'data' => $data,
'w' => $size[0],
'h' => $size[1],
'gray' => $size[2] === 1,
];
if (strncmp($data, "\xFF\xD8", 2) === 0) {
$size = self::jpeg_size($data);
if ($size === null) {
return false;
}
$this->images[$name] = [
'data' => $data,
'w' => $size[0],
'h' => $size[1],
'gray' => $size[2] === 1,
'filter' => 'DCTDecode',
'smask' => null,
];
} elseif (strncmp($data, "\x89PNG\r\n\x1a\n", 8) === 0) {
$png = self::png_decode($data);
if ($png === null) {
return false;
}
$this->images[$name] = [
'data' => gzcompress($png['rgb'], 6),
'w' => $png['w'],
'h' => $png['h'],
'gray' => false,
'filter' => 'FlateDecode',
'smask' => null,
];
if ($png['alpha'] !== null) {
$alphaName = $name . 'A';
$this->images[$alphaName] = [
'data' => gzcompress($png['alpha'], 6),
'w' => $png['w'],
'h' => $png['h'],
'gray' => true,
'filter' => 'FlateDecode',
'smask' => null,
];
$this->images[$name]['smask'] = $alphaName;
}
} else {
return false;
}
$this->stream .= sprintf(
"q %.2F 0 0 %.2F %.2F %.2F cm /%s Do Q\n",
$w,
@ -233,14 +270,20 @@ final class PdfDoc
$next = 5;
$imageRefs = [];
foreach ($this->images as $name => $_) {
$imageRefs[$name] = $next++;
}
foreach ($this->images as $name => $image) {
$imageRefs[$name] = $next;
$objects[$next] = '<< /Type /XObject /Subtype /Image'
$smask = $image['smask'] !== null && isset($imageRefs[$image['smask']])
? ' /SMask ' . $imageRefs[$image['smask']] . ' 0 R'
: '';
$objects[$imageRefs[$name]] = '<< /Type /XObject /Subtype /Image'
. ' /Width ' . $image['w'] . ' /Height ' . $image['h']
. ' /ColorSpace /' . ($image['gray'] ? 'DeviceGray' : 'DeviceRGB')
. ' /BitsPerComponent 8 /Filter /DCTDecode /Length ' . strlen($image['data']) . " >>\nstream\n"
. ' /BitsPerComponent 8 /Filter /' . $image['filter']
. $smask
. ' /Length ' . strlen($image['data']) . " >>\nstream\n"
. $image['data'] . "\nendstream";
$next++;
}
$xobjects = '';
@ -300,10 +343,144 @@ final class PdfDoc
return str_replace(['\\', '(', ')', "\r", "\n"], ['\\\\', '\\(', '\\)', '', ''], $encoded);
}
/**
* Entpackt ein PNG (8 Bit, nicht interlaced, ohne Palette) zu RGB und
* optional Alpha. Die Zeilenfilter nach PNG-Spezifikation (0–4) werden
* dabei rückgängig gemacht.
*
* @return array{w: int, h: int, rgb: string, alpha: ?string}|null
*/
private static function png_decode(string $data): ?array
{
$len = strlen($data);
if ($len < 8 + 25 || strncmp($data, "\x89PNG\r\n\x1a\n", 8) !== 0 || !function_exists('gzuncompress')) {
return null;
}
$pos = 8;
$width = $height = $bitDepth = $colorType = 0;
$interlace = 0;
$idat = '';
$seenHeader = false;
while ($pos + 8 <= $len) {
$chunkLength = unpack('N', substr($data, $pos, 4))[1];
$type = substr($data, $pos + 4, 4);
$body = substr($data, $pos + 8, $chunkLength);
$pos += 12 + $chunkLength;
if ($type === 'IHDR') {
if (strlen($body) < 13) {
return null;
}
$header = unpack('Nw/Nh/Cbit/Ccolor/Ccomp/Cfilter/Cinterlace', substr($body, 0, 13));
$width = (int) $header['w'];
$height = (int) $header['h'];
$bitDepth = (int) $header['bit'];
$colorType = (int) $header['color'];
$interlace = (int) $header['interlace'];
$seenHeader = true;
continue;
}
if ($type === 'IDAT') {
$idat .= $body;
continue;
}
if ($type === 'IEND') {
break;
}
}
if (!$seenHeader || $width < 1 || $height < 1 || $bitDepth !== 8) {
return null;
}
if ($height > 20000 || $width > 20000) {
return null;
}
$channels = match ($colorType) {
0 => 1,
2 => 3,
4 => 2,
6 => 4,
default => 0,
};
if ($channels === 0 || $interlace !== 0 || $idat === '') {
return null;
}
$raw = @gzuncompress($idat);
if (!is_string($raw)) {
return null;
}
$stride = $width * $channels;
if (strlen($raw) < ($stride + 1) * $height) {
return null;
}
$rgb = '';
$alpha = $channels === 2 || $channels === 4 ? '' : null;
$previous = str_repeat("\0", $stride);
$offset = 0;
for ($row = 0; $row < $height; $row++) {
$filter = ord($raw[$offset]);
$line = substr($raw, $offset + 1, $stride);
$offset += $stride + 1;
$pixels = '';
for ($i = 0; $i < $stride; $i++) {
$left = $i >= $channels ? ord($pixels[$i - $channels]) : 0;
$up = ord($previous[$i]);
$upLeft = $i >= $channels ? ord($previous[$i - $channels]) : 0;
$value = ord($line[$i]);
$value = match ($filter) {
0 => $value,
1 => $value + $left,
2 => $value + $up,
3 => $value + (($left + $up) >> 1),
4 => $value + self::paeth($left, $up, $upLeft),
default => $value,
};
$pixels .= chr($value & 0xFF);
}
$previous = $pixels;
for ($x = 0; $x < $width; $x++) {
$base = $x * $channels;
if ($channels === 1 || $channels === 2) {
$gray = $pixels[$base];
$rgb .= $gray . $gray . $gray;
if ($alpha !== null) {
$alpha .= $pixels[$base + 1];
}
continue;
}
$rgb .= $pixels[$base] . $pixels[$base + 1] . $pixels[$base + 2];
if ($alpha !== null) {
$alpha .= $pixels[$base + 3];
}
}
}
return ['w' => $width, 'h' => $height, 'rgb' => $rgb, 'alpha' => $alpha];
}
private static function paeth(int $left, int $up, int $upLeft): int
{
$estimate = $left + $up - $upLeft;
$distanceLeft = abs($estimate - $left);
$distanceUp = abs($estimate - $up);
$distanceUpLeft = abs($estimate - $upLeft);
if ($distanceLeft <= $distanceUp && $distanceLeft <= $distanceUpLeft) {
return $left;
}
return $distanceUp <= $distanceUpLeft ? $up : $upLeft;
}
/**
* Liest Breite, Höhe und Farbkanäle eines JPEG aus dem SOF-Segment.
*
* @return array{0: int, 1: int, 2: int}|null
*
* @return array{0: int, 1: int, 2: int}|null
*/
private static function jpeg_size(string $data): ?array
{