Files
InvoiceShelf/app/Http/Controllers/Admin/FontController.php
Darko Gjorgjijoski ba5c6c39ba Add multilingual PDF font system with Noto Sans and on-demand CJK packages
Bundle Noto Sans (Regular/Bold/Italic/BoldItalic) under resources/static/fonts/ as the default PDF face — it covers Latin, Cyrillic, Greek, Arabic, Thai and Hindi out of the box, replacing the limited DejaVu Sans fallback. Move all @font-face declarations into app.pdf.partials.fonts and include it from every invoice/estimate/payment/report template, dropping per-template font-family hardcodes and the conditional Thai locale include.

Introduce FontService + FontController to download static Noto Sans CJK packages (zh, zh_CN, ja, ko) from life888888/cjk-fonts-ttf on demand. GeneratesPdfTrait::ensureFontsForLocale primes the family before rendering and the partial emits @font-face rules for installed packages so dompdf resolves them through standard CSS — no separate registerFont() instance required. Static TTFs are mandatory because dompdf's PHP-Font-Lib does not parse variable fonts (fvar/gvar tables), which is why Google Fonts' NotoSansTC[wght].ttf rendered empty boxes.

Expose status/install via /api/v1/fonts/status and /api/v1/fonts/{package}/install with matching FONTS_STATUS / FONTS_INSTALL constants in scripts-v2/api/endpoints.ts. Flip DOMPDF_ENABLE_REMOTE default to true for remote asset loading.
2026-04-06 23:32:00 +02:00

53 lines
1.3 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Services\FontService;
use Illuminate\Http\JsonResponse;
class FontController extends Controller
{
public function __construct(
private readonly FontService $fontService,
) {}
public function status(): JsonResponse
{
$this->authorize('manage settings');
return response()->json([
'packages' => $this->fontService->getPackageStatuses(),
]);
}
public function install(string $package): JsonResponse
{
$this->authorize('manage settings');
if (! isset(FontService::FONT_PACKAGES[$package])) {
return response()->json(['error' => 'Unknown font package'], 404);
}
$pkg = FontService::FONT_PACKAGES[$package];
if ($this->fontService->isInstalled($pkg)) {
return response()->json(['success' => true, 'message' => 'Already installed']);
}
try {
$this->fontService->downloadPackage($pkg);
return response()->json([
'success' => true,
'installed' => true,
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'error' => $e->getMessage(),
], 500);
}
}
}