From b338d4e36913c06a3d621e8a486f32d16ccfeb34 Mon Sep 17 00:00:00 2001 From: Eduard Wisch Date: Thu, 9 Apr 2026 15:53:02 +0200 Subject: [PATCH] BerichtPdf: SetFont-Override routet Hack-Styles auf die richtige Family [deploy] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TCPDF_FONTS::addTTFfont registriert jede TTF als eigene Family ohne Styles (hackregular, hackbold, hackitalic, hackbolditalic). SetFont(key,'B') suchte dann nach 'hack.php' und brach mit 'Could not include font definition file' ab. Fix: hack_font_styles-Map ['' / B / I / BI] => family, SetFont-Override mapped den angeforderten Style auf die passende Family mit leerem Style und fällt kaskadierend zurück (BI→B→'', I→''). --- class/berichtpdf.class.php | 41 +++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/class/berichtpdf.class.php b/class/berichtpdf.class.php index 0adca9e..855ef25 100644 --- a/class/berichtpdf.class.php +++ b/class/berichtpdf.class.php @@ -11,10 +11,11 @@ trait BerichtPdfTrait { - public $hack_font_key = 'helvetica'; - public $bericht_title = ''; - public $bericht_company = ''; - public $bericht_logo = ''; + public $hack_font_key = 'helvetica'; + public $hack_font_styles = array(); // style => font-family-key + public $bericht_title = ''; + public $bericht_company = ''; + public $bericht_logo = ''; /** * Muss direkt nach der Konstruktion aufgerufen werden. @@ -37,7 +38,6 @@ trait BerichtPdfTrait 'I' => 'Hack-Italic.ttf', 'BI' => 'Hack-BoldItalic.ttf', ); - $reg_key = null; foreach ($map as $style => $fn) { $src = $fontdir.$fn; if (!file_exists($src)) continue; @@ -46,21 +46,48 @@ trait BerichtPdfTrait if ($k) { $ffile = $outdir.$k.'.php'; if (file_exists($ffile)) { + // WICHTIG: Jede TTF wird als eigene Family mit style='' registriert, + // sonst sucht TCPDF bei SetFont(..., 'B') nach "hackB.php" und + // bricht mit "Could not include font definition file" ab. $this->AddFont($k, '', $ffile); + $this->hack_font_styles[$style] = $k; } - if ($style === '') $reg_key = $k; } } catch (Throwable $e) { error_log('[BerichtPdf] Hack-Font '.$fn.' fehlgeschlagen: '.$e->getMessage()); } } - if ($reg_key) $this->hack_font_key = $reg_key; + if (!empty($this->hack_font_styles[''])) { + $this->hack_font_key = $this->hack_font_styles['']; + } } else { error_log('[BerichtPdf] Font-Outdir nicht schreibbar: '.$outdir); } } } + /** + * Leitet SetFont() um, wenn die Family einer unserer Hack-Varianten ist: + * ein Style-Wechsel ('B'/'I'/'BI') wird auf die passende Hack-Family mit + * leerem Style gemappt. So vermeiden wir den TCPDF-Fallback auf "hack.php". + */ + public function SetFont($family, $style = '', $size = null, $fontfile = '', $subset = 'default', $out = true) + { + $fam = strtolower($family); + $is_hack = in_array($fam, $this->hack_font_styles, true); + if ($is_hack && !empty($this->hack_font_styles)) { + $st = strtoupper(str_replace('U', '', (string) $style)); + if (!isset($this->hack_font_styles[$st])) { + // Kaskade: BI → B → '', I → '' + if ($st === 'BI' && isset($this->hack_font_styles['B'])) $st = 'B'; + elseif (isset($this->hack_font_styles[''])) $st = ''; + } + $target = $this->hack_font_styles[$st] ?? $this->hack_font_styles[''] ?? $family; + return parent::SetFont($target, '', $size, $fontfile, $subset, $out); + } + return parent::SetFont($family, $style, $size, $fontfile, $subset, $out); + } + public function Header() { $pageW = $this->getPageWidth();