PHP code example of tecnickcom / tc-lib-pdf-sign

1. Go to this page and download the library: Download tecnickcom/tc-lib-pdf-sign library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tecnickcom / tc-lib-pdf-sign example snippets


$signer->collectValidationMaterial($chain, $ocsp, $crl, $tokens, $now, function (
    string $source,
    string $url,
    string $reason,
    SkipReason $code,
): void {
    if ($code === SkipReason::Revoked) {
        throw new RuntimeException('Refusing to sign: ' . $reason);
    }

    $this->logger->warning('LTV material skipped', compact('source', 'url', 'reason'));
});

$pdf->signature()->configure([
    'profile'          => 'pades-b-t',   // legacy | pades-b-b | pades-b-t | pades-b-lt | pades-b-lta
    'digest_algorithm' => 'sha256',      // sha256 | sha384 | sha512
    'signcert'         => 'file:///path/to/cert.pem',
    'privkey'          => 'file:///path/to/key.pem',
    'password'         => '',
]);



om\Tecnick\Pdf\Sign\Cms\Builder;

$privateKey = openssl_pkey_get_private('file:///path/to/key.pem');
$certDer    = '';   // DER bytes of the signing certificate
$content    = '';   // detached content bytes (the ByteRange-covered document)

$cms = (new Builder())->sign(
    $content,        // detached content bytes (the ByteRange-covered document)
    $certDer,        // DER of the signing certificate
    $privateKey,     // OpenSSLAsymmetricKey (RSA or EC)
    [],              // additional chain certificates (DER), if any
    'sha256',        // digest algorithm
    time(),          // signing time (Unix timestamp)
);

// $cms is a DER-encoded CMS ContentInfo ready for injection into /Contents.



om\Tecnick\Pdf\Sign\Cms\Builder;
use Com\Tecnick\Pdf\Sign\Cms\SigningRequest;

$builder = new Builder();

// The digest can be computed incrementally, so the content is never held in memory.
$context = hash_init('sha256');
hash_update_stream($context, $stream);

$request = new SigningRequest(
    hash_final($context, true),   // digest of the ByteRange-covered content
    $certDer,                     // DER of the signing certificate
    'sha256',                     // digest algorithm
    time(),                       // signing time (Unix timestamp)
    false,                        // 

(new SignedDataVerifier())->verify($cms, $byteRangeContent);  // returns the signer certificate DER

$state = $request->toArray($secret);          // adds a 'mac' entry
$request = SigningRequest::fromArray($state, $secret);

$cms = $signer->sign(...);                                    // B-T and above
$tokens = $signer->signatureTimestampTokens($cms);
$material = $signer->collectValidationMaterial($chain, $ocsp, $crl, $tokens, $now, $onSkip);