Hack-Font: nur Regular, Styles komplett ignorieren [deploy]
All checks were successful
Deploy bericht / deploy (push) Successful in 1s
All checks were successful
Deploy bericht / deploy (push) Successful in 1s
Problem: TCPDF_FONTS::addTTFfont leitet den Font-Key aus dem PostScript- Family-Namen der TTF ab. Alle 4 Hack-TTFs teilen den internen Namen 'Hack' und erzeugen dieselbe hack.php, die sich gegenseitig überschreibt. SetFont(hack, 'B') sucht dann hackB.php → existiert nicht → 'Could not include font definition file: hack'. Lösung: nur Hack-Regular.ttf registrieren, SetFont-Override erzwingt bei der Hack-Family immer style=''. Header-Titel nutzt nur Schriftgröße statt echter Bold-Variante.
This commit is contained in:
parent
b338d4e369
commit
3ec303c48e
1 changed files with 23 additions and 44 deletions
|
|
@ -27,63 +27,42 @@ trait BerichtPdfTrait
|
|||
$this->bericht_logo = (string) $logo_path;
|
||||
|
||||
// Hack-Font registrieren (Eddys Corporate-Font)
|
||||
// Nur Regular registrieren. Alle 4 Hack-TTFs produzieren in TCPDF die
|
||||
// gleiche interne Family "hack" (PostScript-Name-Kollision) und
|
||||
// überschreiben sich gegenseitig — Bold/Italic sind darum nicht
|
||||
// sauber trennbar. Wir nutzen NUR Regular und ignorieren Styles
|
||||
// komplett (Header-"Bold" wird über Schriftgröße emuliert).
|
||||
if (class_exists('TCPDF_FONTS') && defined('DOL_DATA_ROOT')) {
|
||||
$fontdir = dirname(__DIR__).'/fonts/';
|
||||
$outdir = DOL_DATA_ROOT.'/bericht/tcpdf_fonts/';
|
||||
$src = dirname(__DIR__).'/fonts/Hack-Regular.ttf';
|
||||
$outdir = DOL_DATA_ROOT.'/bericht/tcpdf_fonts/';
|
||||
if (!is_dir($outdir)) @mkdir($outdir, 0755, true);
|
||||
if (is_dir($outdir) && is_writable($outdir)) {
|
||||
$map = array(
|
||||
'' => 'Hack-Regular.ttf',
|
||||
'B' => 'Hack-Bold.ttf',
|
||||
'I' => 'Hack-Italic.ttf',
|
||||
'BI' => 'Hack-BoldItalic.ttf',
|
||||
);
|
||||
foreach ($map as $style => $fn) {
|
||||
$src = $fontdir.$fn;
|
||||
if (!file_exists($src)) continue;
|
||||
try {
|
||||
$k = TCPDF_FONTS::addTTFfont($src, 'TrueTypeUnicode', '', 32, $outdir);
|
||||
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;
|
||||
}
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
error_log('[BerichtPdf] Hack-Font '.$fn.' fehlgeschlagen: '.$e->getMessage());
|
||||
if (file_exists($src) && is_dir($outdir) && is_writable($outdir)) {
|
||||
try {
|
||||
$k = TCPDF_FONTS::addTTFfont($src, 'TrueTypeUnicode', '', 32, $outdir);
|
||||
$ffile = $outdir.$k.'.php';
|
||||
if ($k && file_exists($ffile)) {
|
||||
$this->AddFont($k, '', $ffile);
|
||||
$this->hack_font_key = $k;
|
||||
}
|
||||
}
|
||||
if (!empty($this->hack_font_styles[''])) {
|
||||
$this->hack_font_key = $this->hack_font_styles[''];
|
||||
} catch (Throwable $e) {
|
||||
error_log('[BerichtPdf] Hack-Font fehlgeschlagen: '.$e->getMessage());
|
||||
}
|
||||
} else {
|
||||
error_log('[BerichtPdf] Font-Outdir nicht schreibbar: '.$outdir);
|
||||
error_log('[BerichtPdf] Hack-Font: src='.$src.' outdir='.$outdir.' schreibbar='.(is_writable($outdir)?'ja':'nein'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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".
|
||||
* Erzwingt style='' wenn die Hack-Family angefordert wird. Verhindert
|
||||
* "Could not include font definition file: hack" wenn TCPDFs HTML-Parser
|
||||
* bei <b>/<i>-Tags einen Style-Font-Lookup versucht, für den wir keine
|
||||
* separate Datei haben.
|
||||
*/
|
||||
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);
|
||||
if (strtolower((string) $family) === strtolower((string) $this->hack_font_key) && $this->hack_font_key !== 'helvetica') {
|
||||
return parent::SetFont($this->hack_font_key, '', $size, $fontfile, $subset, $out);
|
||||
}
|
||||
return parent::SetFont($family, $style, $size, $fontfile, $subset, $out);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue