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

1. Go to this page and download the library: Download tecnickcom/tc-lib-pdf-encrypt 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-encrypt example snippets




S-256 R6 (mode 4 — recommended)
$encrypt = new \Com\Tecnick\Pdf\Encrypt\Encrypt(
    true,             // enabled
    md5('unique-file-id'),
    4,                // mode: AES-256 R6 / PDF 2.0
    ['print', 'copy'],
    'userpassword',
    'ownerpassword'
);

$cipher = $encrypt->encryptString('secret payload', $objectNumber = 1);
echo bin2hex($cipher);



ss the encryption dictionary produced by the Encrypt instance.
$decrypt = new \Com\Tecnick\Pdf\Encrypt\Decrypt($encrypt->getEncryptionData());

if ($decrypt->authenticate('userpassword')) {
    $plain = $decrypt->decryptString($cipher, $objectNumber = 1);
    // For AES modes the output is zero-padded to the block size;
    // trim trailing null bytes when the original was not block-aligned.
    echo rtrim($plain, "\x00");
}