打造扩展包
TCPDF-Next 的架构在设计上就是开放的。三个官方扩展包(Artisan、Laravel、Pro)使用的接口与挂载点,和任何第三方开发者可用的完全相同。
接口契约
你的扩展包应实现以下一个或多个接口:
PdfDocumentInterface
核心文档契约。如果你要构建一个替代的文档引擎,请实现此接口。
php
namespace Yeeefang\TcpdfNext\Contracts;
interface PdfDocumentInterface
{
public function addPage(?PageSize $size = null, Orientation $orientation = Orientation::Portrait): static;
public function setMargins(Margin $margin): static;
public function setFont(string $family, string $style = '', float $size = 12.0): static;
public function cell(float $width, float $height, string $text = '', ...): static;
public function multiCell(float $width, float $height, string $text, ...): static;
public function writeHtml(string $html): static;
public function image(string $file, ...): static;
public function output(?string $filename = null, OutputDestination $dest = OutputDestination::Inline): string;
public function save(string $path): void;
}SignerInterface
用于自定义签名后端(云端 HSM、远程签名服务等)。
php
namespace Yeeefang\TcpdfNext\Contracts;
interface SignerInterface
{
public function sign(string $data): SignatureResult;
public function timestamp(string $signatureValue): string;
public function supportsLtv(): bool;
}HsmSignerInterface
用于硬件安全模块集成:
php
namespace Yeeefang\TcpdfNext\Contracts;
interface HsmSignerInterface
{
public function sign(string $data, string $algorithm = 'sha256WithRSAEncryption'): string;
public function getCertificateDer(): string;
public function getCertificateChainDer(): array;
public function getPublicKeyAlgorithm(): string;
}FontManagerInterface
用于自定义字体加载策略:
php
namespace Yeeefang\TcpdfNext\Contracts;
interface FontManagerInterface
{
public function registerFont(string $fontFile, string $alias = '', int $fontIndex = 0): FontInfo;
public function getFont(string $family, string $style = ''): ?FontInfo;
public function subset(FontInfo $font, string $text): string;
public function getRegisteredFonts(): array;
public function addFontDirectory(string $directory): void;
}扩展包骨架
以下是一个最小的第三方扩展包示例:
composer.json
json
{
"name": "your-vendor/tcpdf-next-watermark",
"description": "Advanced watermark extension for TCPDF-Next",
"type": "library",
"require": {
"php": "^8.5",
"yeeefang/tcpdf-next": "^1.7"
},
"autoload": {
"psr-4": {
"YourVendor\\TcpdfNextWatermark\\": "src/"
}
}
}扩展包类
php
namespace YourVendor\TcpdfNextWatermark;
use Yeeefang\TcpdfNext\Core\Document;
use Yeeefang\TcpdfNext\Graphics\Color;
final class WatermarkExtension
{
public function apply(
Document $document,
string $text,
float $angle = 45.0,
float $opacity = 0.15,
): Document {
return $document
->startTransform()
->setAlpha($opacity)
->rotate($angle, $document->getPageWidth() / 2, $document->getPageHeight() / 2)
->setFontSize(60)
->setTextColor(200, 200, 200)
->text(
$document->getPageWidth() / 4,
$document->getPageHeight() / 2,
$text,
)
->stopTransform();
}
}官方扩展包如何接入
Artisan → Core
Chrome 渲染器通过 setChromeRendererConfig() 注入到 Document 类中。Core 将渲染器存储为 ?object——对 Artisan 没有类型依赖。
Laravel → Core
ServiceProvider 为 PdfDocumentInterface 创建工厂绑定,返回一个已配置好的 Document 实例。Facade 将静态调用代理到容器解析的实例上。
Pro → Core
Pro 的类如 LtvManager 和 PdfAManager 操作的是 BinaryBuffer 和 ObjectRegistry——与 Core 自身使用的内部 API 相同。PadesOrchestrator 接受可选的 Pro 专属参数(CertificateChainValidator、OcspResponseVerifier)。
命名空间惯例
遵循生态系的惯例:
YourVendor\TcpdfNext{ExtensionName}\
├── YourMainClass.php
├── Config\
├── Exception\
└── ...公开 API 边界请使用 Yeeefang\TcpdfNext\Contracts\ 的接口,不要使用 Core\ 的内部类。