Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Tc Lib Pdf Font Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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).

Implementation Patterns

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.

Gotchas and Tips

  • No Laravel integration ships with this. No facade, no config publish, no service provider. You must wire it yourself.
  • License is 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.
  • Font paths must be server-readable. In containerized/Forge/Vapor environments, fonts committed to the repo are fine; fonts uploaded at runtime need to land in persistent storage, not storage/framework.
  • Cache invalidation is the classic bug. If you cache metrics, bust the cache when a font file is replaced — hashing the file contents avoids stale metrics after a deploy.
  • Unicode and encoding mismatches cause silent wrong widths. If a glyph is missing, widths will be wrong or fall back oddly. Test with real content (accents, CJK, emoji) rather than "Hello".
  • Pin the version in 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.
  • Don't instantiate per request in a loop. Font objects are stateful; reuse a single instance per font/style/size combination within a job.
  • Debugging: dump the font stack and resolved file paths into a log channel when fonts render unexpectedly — most "wrong font in PDF" reports trace back to a path resolution or style-name mismatch, not this library.
  • Extension points are minimal. Because the API surface is small and undocumented, avoid subclassing library classes; wrap them in your own adapter instead so future upgrades don't ripple through your app.
  • Consider whether you need this at all. If you are not building custom PDF layout logic, just use tc-lib-pdf (or a higher-level package like Laravel Snappy/DomPDF) and let it manage this dependency internally.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
se7enxweb/flysystem
lampwick/laravel
jeslage/statamic-emojis
micahdshackelford/rmap-laravel
draftsmaninc/draftsman
square-bit/laravel-volition
himito/laravel-mailman
htinlynn/genius-helper
maco-studios/hephaestus
elmogy/larafast
xeviant/laravel-paystack
gorilladash/laravel-website-sdk
martialbe/flysystem-oss
chondal/3312client
jeroen-g/flickr
opscale-co/nova-genbi
itsjustvita/laravel-retention
tequilarapido/trackit
gregsphotography/site-builder-filament
cabaoctavio/laravel-query-log