Tham chiếu Interface
Module Contracts (TcpdfNext\Contracts) định nghĩa interface dùng chung mà mọi module TCPDF-Next lập trình theo. Bằng cách phụ thuộc vào contract thay vì class cụ thể, bạn có thể thay thế test double, tạo implement thay thế và duy trì API public ổn định qua các phiên bản chính.
PdfDocumentInterface
Namespace: TcpdfNext\Contracts\PdfDocumentInterface
Contract API chính được implement bởi Document. Bất kỳ class nào đáp ứng interface này có thể được dùng thay thế cho mô hình document tích hợp.
Method
Ví dụ
use TcpdfNext\Contracts\PdfDocumentInterface;
use TcpdfNext\Contracts\OutputDestination;
function generateReport(PdfDocumentInterface $pdf): string
{
return $pdf
->addPage()
->font('Helvetica', size: 14)
->text('Quarterly Report')
->output(OutputDestination::String);
}FontManagerInterface
Namespace: TcpdfNext\Contracts\FontManagerInterface
Định nghĩa tải, đăng ký, subsetting và tra cứu metric font. FontManager tích hợp implement interface này, nhưng bạn có thể cung cấp implement tùy chỉnh cho nguồn font chuyên biệt.
Method
Ví dụ
use TcpdfNext\Contracts\FontManagerInterface;
function addCustomFonts(FontManagerInterface $fonts): void
{
$fonts->registerFont('/fonts/NotoSans-Regular.ttf', 'NotoSans');
$fonts->registerFont('/fonts/NotoSans-Bold.ttf', 'NotoSans');
if ($fonts->hasFont('NotoSans', 'B')) {
$info = $fonts->getFont('NotoSans', 'B');
echo "Ascender: {$info->ascender}";
}
}SignerInterface
Namespace: TcpdfNext\Contracts\SignerInterface
Định nghĩa contract cho bất kỳ nhà cung cấp chữ ký số nào. Implement interface này để tích hợp với chứng chỉ phần mềm, cloud-based key vault hoặc hardware security module (HSM).
Method
Ví dụ
use TcpdfNext\Contracts\SignerInterface;
class FileSigner implements SignerInterface
{
public function __construct(
private readonly string $certPath,
private readonly string $keyPath,
private readonly string $password,
) {}
public function sign(string $data): string
{
$cert = file_get_contents($this->certPath);
$key = openssl_pkey_get_private(
file_get_contents($this->keyPath),
$this->password,
);
openssl_pkcs7_sign(/* ... */);
// Trả về byte chữ ký thô
}
public function certificate(): string { /* ... */ }
public function chain(): array { return []; }
public function estimatedSize(): int { return 8192; }
public function algorithm(): string { return 'sha256WithRSAEncryption'; }
}HsmSignerInterface
Namespace: TcpdfNext\Contracts\HsmSignerInterface
Mở rộng SignerInterface cho hardware security module không bao giờ lộ private key. Thay vì ký dữ liệu thô, HSM ký digest đã tính trước.
Method
Mọi method từ SignerInterface, cộng thêm:
Ví dụ
use TcpdfNext\Contracts\HsmSignerInterface;
class AwsCloudHsmSigner implements HsmSignerInterface
{
public function __construct(
private readonly string $keyId,
private readonly AwsKmsClient $kms,
private readonly string $certPem,
) {}
public function sign(string $data): string
{
$digest = hash('sha256', $data, binary: true);
return $this->signDigest($digest, 'sha256');
}
public function signDigest(string $digest, string $algorithm): string
{
$result = $this->kms->sign([
'KeyId' => $this->keyId,
'Message' => $digest,
'MessageType' => 'DIGEST',
'SigningAlgorithm' => 'RSASSA_PKCS1_V1_5_SHA_256',
]);
return $result['Signature'];
}
public function certificate(): string { return $this->certPem; }
public function chain(): array { return []; }
public function estimatedSize(): int { return 16384; }
public function algorithm(): string { return 'sha256WithRSAEncryption'; }
}Dùng Interface cho Testing
Cả bốn interface được thiết kế để dễ mock với PHPUnit hoặc Mockery.
use PHPUnit\Framework\TestCase;
use TcpdfNext\Contracts\FontManagerInterface;
class TextRendererTest extends TestCase
{
public function testCalculatesWidth(): void
{
$fonts = $this->createMock(FontManagerInterface::class);
$fonts->method('hasFont')->willReturn(true);
$fonts->method('stringWidth')
->with('Hello', 'Helvetica', 12.0, '')
->willReturn(26.64);
$renderer = new TextRenderer($fonts);
$this->assertEqualsWithDelta(26.64, $renderer->width('Hello'), 0.01);
}
}Xem thêm
- Tổng quan API -- Mọi package một cái nhìn
- Tham chiếu Enum -- Enum dùng bởi các interface này (Orientation, Alignment, OutputDestination)
- Document API -- Class cụ thể implement PdfDocumentInterface
- Hướng dẫn bảo mật -- Hướng dẫn đầy đủ về mã hóa và chữ ký số