BerichtPdf: SetFont-Override routet Hack-Styles auf die richtige Family [deploy]
All checks were successful
Deploy bericht / deploy (push) Successful in 1s

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→'').
This commit is contained in:
Eduard Wisch 2026-04-09 15:53:02 +02:00
parent 3a7ed278e5
commit b338d4e369

View file

@ -11,10 +11,11 @@
trait BerichtPdfTrait trait BerichtPdfTrait
{ {
public $hack_font_key = 'helvetica'; public $hack_font_key = 'helvetica';
public $bericht_title = ''; public $hack_font_styles = array(); // style => font-family-key
public $bericht_company = ''; public $bericht_title = '';
public $bericht_logo = ''; public $bericht_company = '';
public $bericht_logo = '';
/** /**
* Muss direkt nach der Konstruktion aufgerufen werden. * Muss direkt nach der Konstruktion aufgerufen werden.
@ -37,7 +38,6 @@ trait BerichtPdfTrait
'I' => 'Hack-Italic.ttf', 'I' => 'Hack-Italic.ttf',
'BI' => 'Hack-BoldItalic.ttf', 'BI' => 'Hack-BoldItalic.ttf',
); );
$reg_key = null;
foreach ($map as $style => $fn) { foreach ($map as $style => $fn) {
$src = $fontdir.$fn; $src = $fontdir.$fn;
if (!file_exists($src)) continue; if (!file_exists($src)) continue;
@ -46,21 +46,48 @@ trait BerichtPdfTrait
if ($k) { if ($k) {
$ffile = $outdir.$k.'.php'; $ffile = $outdir.$k.'.php';
if (file_exists($ffile)) { 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->AddFont($k, '', $ffile);
$this->hack_font_styles[$style] = $k;
} }
if ($style === '') $reg_key = $k;
} }
} catch (Throwable $e) { } catch (Throwable $e) {
error_log('[BerichtPdf] Hack-Font '.$fn.' fehlgeschlagen: '.$e->getMessage()); 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 { } else {
error_log('[BerichtPdf] Font-Outdir nicht schreibbar: '.$outdir); 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() public function Header()
{ {
$pageW = $this->getPageWidth(); $pageW = $this->getPageWidth();