Referensi Interface
Modul Contracts (TcpdfNext\Contracts) mendefinisikan interface bersama yang digunakan semua modul TCPDF-Next sebagai acuan. Dengan bergantung pada kontrak alih-alih class konkret, Anda bisa mengganti test double, membuat implementasi alternatif, dan mempertahankan API publik yang stabil lintas versi major.
PdfDocumentInterface
Namespace: TcpdfNext\Contracts\PdfDocumentInterface
Kontrak API utama yang diimplementasikan oleh Document. Class apa pun yang memenuhi interface ini bisa digunakan sebagai pengganti langsung untuk model dokumen built-in.
Method
Contoh
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
Mendefinisikan loading font, registrasi, subsetting, dan pencarian metrik. FontManager built-in mengimplementasikan interface ini, tetapi Anda bisa menyediakan implementasi kustom untuk sumber font khusus.
Method
Contoh
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
Mendefinisikan kontrak untuk penyedia tanda tangan digital apa pun. Implementasikan interface ini untuk mengintegrasikan dengan sertifikat software, key vault berbasis cloud, atau hardware security module (HSM).
Method
Contoh
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(/* ... */);
// Kembalikan byte tanda tangan mentah
}
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
Meng-extend SignerInterface untuk hardware security module yang tidak pernah mengekspos private key. Alih-alih menandatangani data mentah, HSM menandatangani digest yang sudah dihitung sebelumnya.
Method
Semua method dari SignerInterface, ditambah:
Contoh
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'; }
}Menggunakan Interface untuk Testing
Keempat interface dirancang agar mudah di-mock dengan PHPUnit atau 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);
}
}Lihat Juga
- Ringkasan API -- Semua paket sekilas
- Referensi Enum -- Enum yang digunakan oleh interface ini (Orientation, Alignment, OutputDestination)
- API Document -- Class konkret yang mengimplementasikan PdfDocumentInterface
- Panduan Keamanan -- Panduan lengkap enkripsi dan tanda tangan digital