dompdf fonts

This commit is contained in:
Yurii
2026-02-14 19:05:37 +02:00
parent 3161d0c436
commit 44c20522a0
2 changed files with 65 additions and 1 deletions

View File

@@ -12,6 +12,9 @@
"DejaVu Sans",
"DejaVu Serif",
"DejaVu Sans Mono"
]
],
"additionalParams": {
"fonts": []
}
}
}

View File

@@ -35,8 +35,10 @@ use Dompdf\Options;
use Espo\Core\Utils\Config;
use Espo\Core\Utils\File\Manager as FileManager;
use Espo\Core\Utils\Metadata;
use Espo\Core\Utils\Module;
use Espo\Tools\Pdf\Params;
use Espo\Tools\Pdf\Template;
use RuntimeException;
class DompdfInitializer
{
@@ -62,6 +64,7 @@ class DompdfInitializer
private Config $config,
private Metadata $metadata,
private FileManager $fileManager,
private Module $module,
) {}
public function initialize(Template $template, Params $params): Dompdf
@@ -82,6 +85,8 @@ class DompdfInitializer
$this->fileManager->mkdir($dir);
}
$this->setupFontOptions($options);
$pdf = new Dompdf($options);
$this->mapFonts($pdf, $params->isPdfA(), $dir);
@@ -127,6 +132,8 @@ class DompdfInitializer
return;
}
$this->setupAdditionalFonts($pdf);
/** @var string[] $fontList */
$fontList = $this->metadata->get('app.pdfEngines.Dompdf.fontFaceList') ?? [];
$fontList = array_map(fn ($it) => strtolower($it), $fontList);
@@ -139,4 +146,58 @@ class DompdfInitializer
$fontMetrics->setFontFamily($key, $fontMetrics->getFamily($value));
}
}
private function setupFontOptions(Options $options): void
{
$dirs = ['application/Espo/Resources/fonts'];
foreach ($this->module->getOrderedList() as $module) {
$dirs[] = $this->module->getModulePath($module) . '/Resources/fonts';
}
$dirs[] = 'custom/Espo/Custom/Resources/fonts';
$dirs = array_filter($dirs, fn ($dir) => $this->fileManager->isDir($dir));
$dirs = array_values($dirs);
$options->setChroot($dirs);
}
private function setupAdditionalFonts(Dompdf $pdf): void
{
/** @var array{family?: string, style?: string, weight?: string, source?: string}[] $fonts */
$fonts = $this->metadata->get("app.pdfEngines.Dompdf.additionalParams.fonts") ?? [];
foreach ($fonts as $defs) {
$family = $defs['family'] ?? throw new RuntimeException("Not font 'family'.");
$style = $defs['style'] ?? throw new RuntimeException("Not font 'style'.");
$weight = $defs['weight'] ?? throw new RuntimeException("Not font 'weight'.");
$source = $defs['source'] ?? throw new RuntimeException("Not font 'source'.");
$this->registerFont(
pdf: $pdf,
family: $family,
style: $style,
weight: $weight,
source: $source,
);
}
}
private function registerFont(
Dompdf $pdf,
string $family,
string $style,
string $weight,
string $source,
): void {
$fontMetrics = $pdf->getFontMetrics();
$fontMetrics->registerFont([
'family' => $family,
'style' => $style,
'weight' => $weight,
], $source);
}
}