bericht/class/berichtpdf.class.php
Eduard Wisch 3ec303c48e
All checks were successful
Deploy bericht / deploy (push) Successful in 1s
Hack-Font: nur Regular, Styles komplett ignorieren [deploy]
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.
2026-04-09 15:58:09 +02:00

114 lines
4.6 KiB
PHP

<?php
/**
* BerichtPdf — TCPDF/FPDI-Subclass mit Header, Footer und Hack-Font-Setup
* für das Bericht-Modul.
*
* Header: links Bericht-Titel + Firmenname, rechts Firmen-Logo, Trennlinie.
* Footer: zentriert "Seite X / Y".
* Font: Hack-TTFs werden in ein beschreibbares DOL_DATA_ROOT/bericht/tcpdf_fonts/
* kompiliert und per AddFont an die PDF-Instance gebunden.
*/
trait BerichtPdfTrait
{
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.
*/
public function berichtInit($title, $company, $logo_path)
{
$this->bericht_title = (string) $title;
$this->bericht_company = (string) $company;
$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')) {
$src = dirname(__DIR__).'/fonts/Hack-Regular.ttf';
$outdir = DOL_DATA_ROOT.'/bericht/tcpdf_fonts/';
if (!is_dir($outdir)) @mkdir($outdir, 0755, true);
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;
}
} catch (Throwable $e) {
error_log('[BerichtPdf] Hack-Font fehlgeschlagen: '.$e->getMessage());
}
} else {
error_log('[BerichtPdf] Hack-Font: src='.$src.' outdir='.$outdir.' schreibbar='.(is_writable($outdir)?'ja':'nein'));
}
}
}
/**
* 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)
{
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);
}
public function Header()
{
$pageW = $this->getPageWidth();
$logo_w = 0;
if ($this->bericht_logo && file_exists($this->bericht_logo)) {
$info = @getimagesize($this->bericht_logo);
if ($info && $info[0] && $info[1]) {
$maxW = 40; $maxH = 18;
$ratio = min($maxW / $info[0], $maxH / $info[1]);
$w = $info[0] * $ratio;
$h = $info[1] * $ratio;
$this->Image($this->bericht_logo, $pageW - 10 - $w, 6, $w, $h);
$logo_w = $w;
}
}
$this->SetTextColor(40, 40, 40);
$this->SetFont($this->hack_font_key, 'B', 13);
$this->SetXY(10, 8);
$textW = $pageW - 20 - $logo_w - 4;
$this->Cell($textW, 6, $this->bericht_title, 0, 2, 'L');
if ($this->bericht_company) {
$this->SetFont($this->hack_font_key, '', 9);
$this->SetX(10);
$this->Cell($textW, 5, $this->bericht_company, 0, 2, 'L');
}
$this->SetDrawColor(180, 180, 180);
$this->SetLineWidth(0.2);
$this->Line(10, 27, $pageW - 10, 27);
}
public function Footer()
{
$this->SetY(-12);
$this->SetFont($this->hack_font_key, '', 9);
$this->SetTextColor(100, 100, 100);
$txt = 'Seite '.$this->getAliasNumPage().' / '.$this->getAliasNbPages();
$this->Cell(0, 8, $txt, 0, 0, 'C');
}
}
class BerichtPdf extends TCPDF { use BerichtPdfTrait; }
if (class_exists('\\setasign\\Fpdi\\Tcpdf\\Fpdi')) {
class BerichtPdfFpdi extends \setasign\Fpdi\Tcpdf\Fpdi { use BerichtPdfTrait; }
}