Encrypt

Encrypts data for PDF documents.

Namespace: Com\Tecnick\Pdf\Encrypt

class Encrypt extends Compute

Extends: Compute

Source: src/Encrypt.php:38

Encrypts data for PDF documents.

Methods

__construct()

Set PDF document protection (permission settings).

public __construct(
    bool $enabled = false,
    string $file_id = '',
    int $mode = 0,
    string[] $permissions = \self::DEFAULTPERMS,
    string $user_pass = '',
    string $owner_pass = '',
    ?list<array{'c': string, 'p'?: string[]}> $pubkeys = null,
    bool $encryptMetadata = true,
    bool $encryptEmbeddedFiles = true
)

Parameters:

  • $enabled (bool): False if the encryption is disabled.
  • $file_id (string): File ID as an even-length hexadecimal string; a random one is generated when empty. It is the first element of the trailer /ID array, from which revisions 2 to 4 derive the key.
  • $mode (int): Encryption strength: 0 = RC4-40 (deprecated); 1 = RC4-128 (deprecated); 2 = AES-128; 3 = AES-256 R5; 4 = AES-256 R6 (PDF 2.0 / ISO 32000-2).
  • $permissions (string[]): The set of permissions to block: ‘owner’ (inverted logic: when set, permits change of encryption), ‘print’, ‘modify’, ‘copy’, ‘annot-forms’, ‘fill-forms’, ’extract’, ‘assemble’, ‘print-high’.
  • $user_pass (string): User password.
  • $owner_pass (string): Owner password. A random value is used when empty.
  • $pubkeys (?list<array{‘c’: string, ‘p’?: string[]}>): Recipients, each with a public-key certificate (‘c’) and the permissions to block for it (‘p’), for example: [[‘c’ => ‘file://cert.pem’, ‘p’ => [‘print’]]].
  • $encryptMetadata (bool): When false, adds /EncryptMetadata false to the encryption dictionary and leaves metadata streams unencrypted. Requires mode 2, 3 or 4: for modes 0 and 1 the value is forced back to true with an E_USER_WARNING.
  • $encryptEmbeddedFiles (bool): Selects the /EFF crypt filter for V 4 and V 5. True omits /EFF, so embedded file streams use the /StmF filter; false writes /EFF /Identity, and the caller must then write embedded file streams without calling encryptString() on them.

Throws:

Source: src/Encrypt.php:69

convertHexStringToString()

Convert hexadecimal string to string.

public convertHexStringToString(string $bstr): string

Parameters:

  • $bstr (string): Byte-string to convert.

Returns: string

Throws:

  • Exception: When the input is not an even-length hexadecimal string.

Source: src/Encrypt.php:336

convertStringToHexString()

Convert string to hexadecimal string (byte string).

public convertStringToHexString(string $str): string

Parameters:

  • $str (string): String to convert.

Returns: string

Source: src/Encrypt.php:359

encodeNameObject()

Encode a name object.

Every character outside [0-9a-zA-Z_=-], including the NUMBER SIGN itself, is written as a two-digit #XX escape (ISO 32000-1 section 7.3.5).

public encodeNameObject(string $name): string

Parameters:

  • $name (string): Name object to encode.

Returns: string

Source: src/Encrypt.php:372

encryptString()

Encrypt a string.

public encryptString(string $str, int $objnum = 0, int $gennum = 0): string

Parameters:

  • $str (string): String to encrypt.
  • $objnum (int): Object ID.
  • $gennum (int): Object generation number.

Returns: string

Throws:

Source: src/Encrypt.php:398

escapeDataString()

Format a data string for meta information.

public escapeDataString(string $str, int $objnum = 0, int $gennum = 0): string

Parameters:

  • $str (string): Data string to escape.
  • $objnum (int): Object ID.
  • $gennum (int): Object generation number.

Returns: string

Throws:

Source: src/Encrypt.php:412

getEncryptionData()

Get the encryption data array.

public getEncryptionData(): TEncryptData

Returns: TEncryptData

Source: src/Encrypt.php:313

getFileId()

Return the file ID as a hexadecimal string.

The document must carry this value as the first element of the trailer /ID array: revisions 2 to 4 derive the encryption key from it.

public getFileId(): string

Returns: string

Source: src/Encrypt.php:324

getFormattedDate()

Returns a formatted date-time.

The instant is rendered in UTC, not in the ambient date.timezone.

public getFormattedDate(int $time = null, int $objnum = 0, int $gennum = 0): string

Parameters:

  • $time (int): UTC time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
  • $objnum (int): Object ID.
  • $gennum (int): Object generation number.

Returns: string: escaped date string.

Throws:

Source: src/Encrypt.php:430

Inherited members

From Compute

From Data

  • DEFAULTPERMS: Permission set applied when the caller does not choose one.

From Output