承接 tecnickcom/tc-lib-pdf-font 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tecnickcom/tc-lib-pdf-font

Composer 安装命令:

composer require tecnickcom/tc-lib-pdf-font

包简介

PHP library containing PDF page formats and definitions

README 文档

README

Font import, metrics, and stack management utilities for PDF generation.

Latest Stable Version Build Coverage License Downloads

Sponsor on GitHub

💖 Part of the tc-lib-pdf / TCPDF ecosystem (100M+ installs). Sponsor its maintenance →

Overview

tc-lib-pdf-font provides font import and runtime font-stack services used by PDF composition engines.

It bridges static font assets and runtime document composition by handling metrics, encodings, and font program references in a PDF-friendly way. This modular design lets applications evolve font workflows independently from the rest of the rendering stack.

Namespace \Com\Tecnick\Pdf\Font
Author Nicola Asuni info@tecnick.com
License GNU LGPL v3 - see LICENSE
API docs https://tcpdf.org/docs/srcdoc/tc-lib-pdf-font
Packagist https://packagist.org/packages/tecnickcom/tc-lib-pdf-font

Features

Font Processing

  • Import support for core, Type1, and TrueType sources
  • Font metadata extraction and normalization
  • Utilities for subset and output dictionary generation
  • Optional injectable cache to reuse computed TrueType font subsets

Runtime Font Stack

  • Font stack insertion and switching
  • Glyph width/bounding-box helpers
  • Character replacement and fallback handling

Requirements

  • PHP 8.2 or later
  • Extensions: json, zlib
  • Composer

Installation

composer require tecnickcom/tc-lib-pdf-font

Quick Start

<?php

require_once __DIR__ . '/vendor/autoload.php';

$font = new \Com\Tecnick\Pdf\Font\Import('/path/to/font.ttf');
$metrics = $font->getFontMetrics();

var_dump($font->getFontName(), $metrics['type']);

For larger examples, refer to test/OutputTest.php and the conversion tooling in this repository.

Subset Caching

TrueType font subsetting is computational and memory intensive. When the same fonts and character sets are embedded repeatedly (for example across many generated documents), you can inject an optional cache into \Com\Tecnick\Pdf\Font\Output to reuse previously computed subset programs.

Provide any object implementing \Com\Tecnick\Pdf\Font\FontSubsetCacheInterface:

interface FontSubsetCacheInterface
{
    public function get(string $key): ?string;          // null on cache miss
    public function set(string $key, string $subsetFont): void;
}

Pass it as the last constructor argument:

$output = new \Com\Tecnick\Pdf\Font\Output(
    $fonts,
    $objectNumber,
    $encrypt,
    $fileHelper,   // or null
    $subsetCache,  // your FontSubsetCacheInterface implementation
);

When no cache is supplied (the default), behavior is unchanged and every subset is recomputed. The library never evicts entries — the injected backend owns expiration and size limits. The cache key already accounts for the font program bytes, the cmap-selection metrics, and the requested subset characters, so distinct inputs never collide.

Using a PSR-16 cache

No PSR-16 adapter is bundled, because the interface above is intentionally trivial to wrap around any backend (PSR-16/PSR-6, Symfony Cache, Laravel, Redis, APCu, …). A correct PSR-16 wrapper is a few lines:

use Com\Tecnick\Pdf\Font\FontSubsetCacheInterface;
use Psr\SimpleCache\CacheInterface;

final class Psr16SubsetCache implements FontSubsetCacheInterface
{
    public function __construct(private CacheInterface $cache) {}

    public function get(string $key): ?string
    {
        $value = $this->cache->get($this->normalize($key));

        return \is_string($value) ? $value : null;
    }

    public function set(string $key, string $subsetFont): void
    {
        $this->cache->set($this->normalize($key), $subsetFont);
    }

    /**
     * PSR-16 only guarantees keys matching [A-Za-z0-9_.] up to 64 chars and
     * reserves {}()/\@: — the library's raw key uses ':' and '-' and is longer,
     * so hash it into a guaranteed-legal, fixed-length key.
     */
    private function normalize(string $key): string
    {
        return 'tclpf_' . \sha1($key);
    }
}

The normalize() step matters: the library's raw key is human-readable and works with permissive backends, but stricter PSR-16 implementations may reject or mangle it. Hashing it keeps the wrapper portable. Note that hashing discards the readable namespace, so scope the underlying PSR-16 pool to this library if you share it with other consumers.

Converting Existing Fonts

Use the CLI utilities in util/ to convert existing font files into the JSON/Z format consumed by this library.

Convert One or More Fonts

Run util/convert.php and pass one or more input files with --fonts:

php util/convert.php \
	--outpath=./target/fonts/custom/ \
	--type=TrueTypeUnicode \
	--flags=32 \
	--encoding_id=1 \
	--fonts=/path/to/MyFont-Regular.ttf,/path/to/MyFont-Bold.ttf

The command writes generated font definition files to --outpath.

Common options:

  • --type: Explicit font type (TrueTypeUnicode, TrueType, Type1, CID0JP, CID0KR, CID0CS, CID0CT). Leave empty for autodetect.
  • --encoding: Encoding table (for example cp1252 for many non-Unicode Type1/Core cases). Omit for Unicode and symbolic fonts.
  • --flags: PDF descriptor flags. Default is 32 (non-symbolic).
  • --platform_id and --encoding_id: CMAP selection for TrueType Unicode imports (defaults: 3 and 1).
  • --linked: Link to system font file instead of embedding/copying it (not transportable).

To see full usage help:

php util/convert.php --help

Bulk Conversion

For batch generation from the mirrored font set:

cd util
make build

This installs util dependencies and runs bulk_convert.php, which scans the mirror package and writes converted fonts under target/fonts/.

Notes:

  • bulk_convert.php also attempts OTF conversion via FontForge (fontforge -script otf2ttf.ff ...) before import.
  • If you run bulk conversion directly, customize destination with php util/bulk_convert.php --outpath=/your/path/.

Development

make deps
make help
make qa

Font generation helpers are also available through Make targets such as fonts.

Packaging

make rpm
make deb

For system packages, bootstrap with:

require_once '/usr/share/php/Com/Tecnick/Pdf/Font/autoload.php';

Contributing

Contributions are welcome. Please review CONTRIBUTING.md, CODE_OF_CONDUCT.md, and SECURITY.md.

tecnickcom/tc-lib-pdf-font 适用场景与选型建议

tecnickcom/tc-lib-pdf-font 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 934.62k 次下载、GitHub Stars 达 21, 最近一次更新时间为 2015 年 12 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「import」 「pdf」 「font」 「afm」 「ttf」 「tc-lib-pdf-font」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 tecnickcom/tc-lib-pdf-font 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tecnickcom/tc-lib-pdf-font 我们能提供哪些服务?
定制开发 / 二次开发

基于 tecnickcom/tc-lib-pdf-font 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 934.62k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 21
  • 点击次数: 42
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 21
  • Watchers: 4
  • Forks: 10
  • 开发语言: PHP

其他信息

  • 授权协议: LGPL-3.0-or-later
  • 更新时间: 2015-12-01