tecnickcom/tc-lib-pdf-font
Font import, metrics, and runtime font-stack utilities for PDF generation in PHP. Supports core, Type1, and TrueType fonts, glyph width and bounding-box helpers, character fallback, and composite font encoding.
This is a low-level font engine, not a standalone Laravel package. In practice you will almost never install it directly for app features — it is pulled in transitively by tecnickcom/tc-lib-pdf. Install it explicitly only when you need to import fonts, read metrics, or inspect font stacks outside a full PDF render:
composer require tecnickcom/tc-lib-pdf-font
The package is PSR-4 autoloaded under the Com\Tecnick\Pdf\Font namespace. There is no service provider, config file, or facade. Start by reading src/ and the tests/ directory — the test suite is effectively the API reference for this library, since the README is a marketing overview rather than a usage guide.
First useful use case in a Laravel app: measuring text before layout — e.g. deciding whether a product name fits a fixed-width PDF column, or computing dynamic row heights:
use Com\Tecnick\Pdf\Font\Font; // verify exact class in src/
$font = new Font('helvetica', '', 12);
$width = $font->getStringWidth('Some product name');
Treat any direct usage as an internal utility. Wrap it behind your own service so you can swap implementations if the package stalls (22 stars, 0 dependents, single-maintainer ecosystem).
Use it through tc-lib-pdf, not alone. The intended day-to-day path is registering fonts on the PDF engine, which delegates to this library. Add custom TTFs by pointing the engine at a font directory (normally storage/app/fonts) and calling addFont()/setFont() on the PDF object, not on this package.
Bind a wrapper in the container so controllers and jobs stay testable:
// app/Services/Pdf/FontService.php
final class FontService
{
public function metrics(string $family, string $style, float $size): array
{
// delegate to tc-lib-pdf-font classes here
}
public function width(string $text, string $family, float $size): float
{
// ...
}
}
// AppServiceProvider
$this->app->singleton(FontService::class);
Cache font metrics aggressively. Font parsing and metric extraction is the expensive part, and font files rarely change. Key the cache by a hash of the font file:
$key = 'pdf:font-metrics:'.md5_file(storage_path('app/fonts/Inter-Regular.ttf'));
$metrics = Cache::rememberForever($key, fn () => $this->extractMetrics($path));
Config-driven font registry. Keep a config/pdf-fonts.php map of family → style → file path, and resolve it in a small resolver class. This avoids scattering raw paths and makes it easy to swap DejaVu for Inter or Noto later.
Run PDF/font work in queued jobs when generating large documents or importing many TTFs — font parsing can take hundreds of milliseconds per file and should not sit in a request cycle.
Testing: avoid loading real font files in unit tests. Mock your FontService wrapper; reserve real font loading for a small integration test that asserts a known string width within a tolerance.
NOASSERTION on Packagist — resolve this before commercial use. Check the LICENSE file in the repo and confirm with the TCPDF ecosystem licensing (typically LGPL-family), because a compliance review will block on the Packagist metadata alone.storage/framework."Hello".composer.json ("tecnickcom/tc-lib-pdf-font": "^x.y") and pair it with a compatible tc-lib-pdf version. The TCPDF ecosystem has had breaking renames across major versions.tc-lib-pdf (or a higher-level package like Laravel Snappy/DomPDF) and let it manage this dependency internally.How can I help you explore Laravel packages today?